如何增加 linq 语句中的索引值。
int headIndex = -1;
// int itemIndex = -1;
lst = (from xx in db.vwCustomizationHeaders
where xx.ProductID == pID
select new custHeader()
{
headIndex = headIndex++,// Guid.NewGuid(),
}
当你在creating代码中的此查询:
from xx in db.vwCustomizationHeaders
where xx.ProductID == pID
select new custHeader()
{
headIndex = headIndex++
}
其实是executed在数据库中。并且数据库无法修改代码中的值。所以你不能增加代码本地值(headIndex
)来自数据库。 (此外,正如 @Kirk Woll 指出的那样,在选择中修改值是非常糟糕的做法。选择应该只获取/构建某些内容,而不是改变状态或产生副作用。)
如果您所做的只是更新该值,则不需要使用选择。您可以直接将记录数添加到该值中:
headIndex += db.vwCustomizationHeaders.Count(ch => ch.ProductID == pID);
注释掉的部分表明您还在构建一个列表vwCustomizationHeader
不过,是这样的:
lst = (from xx in db.vwCustomizationHeaders
where xx.ProductID == pID
select new custHeader()
{
SomeField = xx.SomeField,
AnotherField = xx.SomeOtherField
// etc.
});
从那里您可以使用lst
对象修改你的计数器:
headIndex += lst.Count();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)