Foreach 更新 LINQ 结果中的每条记录
Foreach updates every record in a LINQ result
我使用 WCF 数据服务通过分页获取数据。
我必须为客户提供一个特定的对象(ExtraData),但是在数据库中,没有这样的数据。它是几个表的组合数据,它有 1 行以使其可交叉连接。
由于 WCF 数据服务不允许创建动态对象,returned 数据必须在查询中。
服务return以客户要求的形式更新数据。
我有以下方法:
[WebGet]
public IQueryable<ExtraData> GetExtraData(string groupID)
{
var query= (from data in context.Data
join information in context.Information on information.ID equals data.InformationID into tempInformation
from information in tempInformation.DefaultIfEmpty()
from extraData in context.ExtraData // cross-joining the dummy
where data.GroupID == groupID
select new
{
ExtraData = extraData,
Data = data,
InformationText = information.Text
}).ToList();
//After the execution, I intend to modify the result (as it is a dummy record yet):
query.ForEach(
item =>
{
item.ExtraData.DataID = item.Data.ID;
item.ExtraData.Name = item.Data.NameAux;
item.ExtraData.Group = elem.Data.ExtraGroup;
}
);
return (from item in query
select item.ExtraData).AsQueryable();
}
不幸的是,它每次都会修改每条记录,所以我最终得到了同一条记录多次。
我应该修改什么以使 ExtraData
记录唯一?
更新:
在 foreach 中,我得到了这个数据:
3ca65876-c88f-4849-bef5-170e62f084ec Name16
b705ebc3-8245-4c16-8045-a79ef15192d2 Name16
b8bb423c-02ff-4e9a-b941-a20a9c69dd12 Name Second 16
4e3d3496-4b36-4dab-b471-a43ffb075345 Other16
f93a2358-818e-4929-a51a-46a7b7080bd4 Test16
a4bca994-73d2-4d0e-a18a-2539067a7498 Test Second 16
c7474a92-430a-46ad-bc3d-7e526dfb2647 New Test 16
6117f1b6-3f6b-4fae-b448-2778d68d0877 New Test Mod 16
8e831455-4305-4ee3-b56d-3b0e23131df8 Test Mod 16
在结果集中,我得到这个:
<entry><id>http://localhost/MyService/Service.svc/ExtraData(guid'8e831455-4305-4ee3-b56d-3b0e23131df8')</id><category term="ExtraData" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="ExtraData" href="ExtraData(guid'8e831455-4305-4ee3-b56d-3b0e23131df8')" /><title /><updated>2015-11-10T10:07:36Z</updated><author><name /></author><content type="application/xml"><m:properties><d:ID m:type="Edm.Guid">8e831455-4305-4ee3-b56d-3b0e23131df8</d:ID><d:Name>Test Mod 16</d:Name><d:Group m:type="Edm.Int32">1</d:Group></m:properties></content></entry>
<entry><id>http://localhost/MyService/Service.svc/ExtraData(guid'8e831455-4305-4ee3-b56d-3b0e23131df8')</id><category term="ExtraData" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="DSPaciens" href="ExtraData(guid'8e831455-4305-4ee3-b56d-3b0e23131df8')" /><title /><updated>2015-11-10T10:07:36Z</updated><author><name /></author><content type="application/xml"><m:properties><d:ID m:type="Edm.Guid">8e831455-4305-4ee3-b56d-3b0e23131df8</d:ID><d:Name>Test Mod 16</d:Name><d:Group m:type="Edm.Int32">1</d:Group></m:properties></content></entry>
嗯,我们非常接近解决方案:)
关键要素是@usr 写的:
There's just one such object per row in the database. You're writing
the the same objects many times.
不幸的是,所选的匿名对象是只读的,所以我不得不创建一个包装器-class来保存必要的数据:
public class CombinedData
{
public ExtraData ExtraData { get; set; }
public Data Data { get; set; }
public string InformationText {get; set; }
}
然后在查询中使用它:
var query= (from data in context.Data
join information in context.Information on information.ID equals data.InformationID into tempInformation
from information in tempInformation.DefaultIfEmpty()
from extraData in context.ExtraData // cross-joining the dummy
where data.GroupID == groupID
select new CombinedData
{
ExtraData = extraData,
Data = data,
InformationText = information.Text
}).ToList();
然后在 ForEach
循环中创建一个新的 ExtraData 对象:
query.ForEach(
item =>
{
item.ExtraData=new ExtraData();
item.ExtraData.DataID = item.Data.ID;
item.ExtraData.Name = item.Data.NameAux;
item.ExtraData.Group = elem.Data.ExtraGroup;
}
);
现在,它起作用了。感谢您为我指明正确的方向:)
我使用 WCF 数据服务通过分页获取数据。
我必须为客户提供一个特定的对象(ExtraData),但是在数据库中,没有这样的数据。它是几个表的组合数据,它有 1 行以使其可交叉连接。
由于 WCF 数据服务不允许创建动态对象,returned 数据必须在查询中。
服务return以客户要求的形式更新数据。
我有以下方法:
[WebGet]
public IQueryable<ExtraData> GetExtraData(string groupID)
{
var query= (from data in context.Data
join information in context.Information on information.ID equals data.InformationID into tempInformation
from information in tempInformation.DefaultIfEmpty()
from extraData in context.ExtraData // cross-joining the dummy
where data.GroupID == groupID
select new
{
ExtraData = extraData,
Data = data,
InformationText = information.Text
}).ToList();
//After the execution, I intend to modify the result (as it is a dummy record yet):
query.ForEach(
item =>
{
item.ExtraData.DataID = item.Data.ID;
item.ExtraData.Name = item.Data.NameAux;
item.ExtraData.Group = elem.Data.ExtraGroup;
}
);
return (from item in query
select item.ExtraData).AsQueryable();
}
不幸的是,它每次都会修改每条记录,所以我最终得到了同一条记录多次。
我应该修改什么以使 ExtraData
记录唯一?
更新:
在 foreach 中,我得到了这个数据:
3ca65876-c88f-4849-bef5-170e62f084ec Name16
b705ebc3-8245-4c16-8045-a79ef15192d2 Name16
b8bb423c-02ff-4e9a-b941-a20a9c69dd12 Name Second 16
4e3d3496-4b36-4dab-b471-a43ffb075345 Other16
f93a2358-818e-4929-a51a-46a7b7080bd4 Test16
a4bca994-73d2-4d0e-a18a-2539067a7498 Test Second 16
c7474a92-430a-46ad-bc3d-7e526dfb2647 New Test 16
6117f1b6-3f6b-4fae-b448-2778d68d0877 New Test Mod 16
8e831455-4305-4ee3-b56d-3b0e23131df8 Test Mod 16
在结果集中,我得到这个:
<entry><id>http://localhost/MyService/Service.svc/ExtraData(guid'8e831455-4305-4ee3-b56d-3b0e23131df8')</id><category term="ExtraData" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="ExtraData" href="ExtraData(guid'8e831455-4305-4ee3-b56d-3b0e23131df8')" /><title /><updated>2015-11-10T10:07:36Z</updated><author><name /></author><content type="application/xml"><m:properties><d:ID m:type="Edm.Guid">8e831455-4305-4ee3-b56d-3b0e23131df8</d:ID><d:Name>Test Mod 16</d:Name><d:Group m:type="Edm.Int32">1</d:Group></m:properties></content></entry>
<entry><id>http://localhost/MyService/Service.svc/ExtraData(guid'8e831455-4305-4ee3-b56d-3b0e23131df8')</id><category term="ExtraData" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="DSPaciens" href="ExtraData(guid'8e831455-4305-4ee3-b56d-3b0e23131df8')" /><title /><updated>2015-11-10T10:07:36Z</updated><author><name /></author><content type="application/xml"><m:properties><d:ID m:type="Edm.Guid">8e831455-4305-4ee3-b56d-3b0e23131df8</d:ID><d:Name>Test Mod 16</d:Name><d:Group m:type="Edm.Int32">1</d:Group></m:properties></content></entry>
嗯,我们非常接近解决方案:)
关键要素是@usr 写的:
There's just one such object per row in the database. You're writing the the same objects many times.
不幸的是,所选的匿名对象是只读的,所以我不得不创建一个包装器-class来保存必要的数据:
public class CombinedData
{
public ExtraData ExtraData { get; set; }
public Data Data { get; set; }
public string InformationText {get; set; }
}
然后在查询中使用它:
var query= (from data in context.Data
join information in context.Information on information.ID equals data.InformationID into tempInformation
from information in tempInformation.DefaultIfEmpty()
from extraData in context.ExtraData // cross-joining the dummy
where data.GroupID == groupID
select new CombinedData
{
ExtraData = extraData,
Data = data,
InformationText = information.Text
}).ToList();
然后在 ForEach
循环中创建一个新的 ExtraData 对象:
query.ForEach(
item =>
{
item.ExtraData=new ExtraData();
item.ExtraData.DataID = item.Data.ID;
item.ExtraData.Name = item.Data.NameAux;
item.ExtraData.Group = elem.Data.ExtraGroup;
}
);
现在,它起作用了。感谢您为我指明正确的方向:)