C# 数据表和更新记录的Linq查询
我在内存中有一个数据表,我需要从中选择一些记录,遍历对字段进行更改的记录,并将更改返回到数据表。我可以使用过滤器、视图和sql来实现这一点,但我正在尝试使用LinqC# 数据表和更新记录的Linq查询,c#,linq,C#,Linq,我在内存中有一个数据表,我需要从中选择一些记录,遍历对字段进行更改的记录,并将更改返回到数据表。我可以使用过滤器、视图和sql来实现这一点,但我正在尝试使用Linq var results = (from rows in dtTheRows.AsEnumerable() select new { rows.Job,
var results = (from rows in dtTheRows.AsEnumerable()
select new
{
rows.Job,
}).Distinct();
foreach (var row in results)
{
firstRow = true;
thisOnHand = 0;
var here = from thisRow in dtTheRows.AsEnumerable()
orderby thisRow.PromisedDate
select new
{
thisRow.OnHandQuantity,
thisRow.Balance,
thisRow.RemainingQuantity
};
foreach(var theRow in here)
{
// business logic here ...
theRow.OnHandQuantity = 5;
} // foreach ...
第一个linq查询和foreach获得要考虑的数据子集列表。我把它包括在这里,以防它是相关的。我的问题在这一行:
heRow.OnHandQuantity = 5;
我的错误是:
错误19无法将属性或索引器“AnonymousType1.OnHandQuantity”分配给-它是只读的
我错过了什么?我可以将此查询更新回原始数据表吗?错误是自描述性的,您不能更新/修改匿名类型。必须从查询中返回要修改的原始实体
select thisRow;
而不是
select new
{
thisRow.OnHandQuantity,
thisRow.Balance,
thisRow.RemainingQuantity
};
不要在select中传递三个变量,而是传递此行本身。这可能会解决语句错误-theRow.OnHandQuantity=5 可能重复的
var here = from thisRow in dtTheRows.AsEnumerable()
orderby thisRow.PromisedDate
select new
{
thisRow.OnHandQuantity,
thisRow.Balance,
thisRow.RemainingQuantity
};