表达式树可能不包含赋值运算符? [英] An expression tree may not contain an assignment operator?
问题描述
如何在linq语句中增加索引值.
How can i increment the index value in linq statement.
int headIndex = -1;
// int itemIndex = -1;
lst = (from xx in db.vwCustomizationHeaders
where xx.ProductID == pID
select new custHeader()
{
headIndex = headIndex++,// Guid.NewGuid(),
}
推荐答案
在代码中创建该查询时,
from xx in db.vwCustomizationHeaders
where xx.ProductID == pID
select new custHeader()
{
headIndex = headIndex++
}
它实际上是在数据库中执行的.而且数据库无法修改代码中的值.因此,您无法从数据库中递增该代码局部值(headIndex
). (此外,正如@Kirk Woll指出的那样,修改选择中的值是非常不好的做法.选择应仅获取/构建某些东西,而不改变状态或产生副作用.)
It's actually executed at the database. And the database can't modify values in your code. So you can't increment that code-local value (headIndex
) from the database. (Additionally, as @Kirk Woll pointed out, it's very bad practice to modify values like that in a select. A select should just fetch/build something, not alter state or produce side-effects.)
如果您要做的只是更新该值,则无需使用select.您可以将记录数直接添加到值中:
If all you're doing is updating that value, you don't need to use a select. You can add the count of records to the value directly:
headIndex += db.vwCustomizationHeaders.Count(ch => ch.ProductID == pID);
已注释掉的部分表明您虽然也在构建vwCustomizationHeader
的列表,如下所示:
The commented-out part suggests that you're also building a list of vwCustomizationHeader
s though, something like this:
lst = (from xx in db.vwCustomizationHeaders
where xx.ProductID == pID
select new custHeader()
{
SomeField = xx.SomeField,
AnotherField = xx.SomeOtherField
// etc.
});
从那里,您可以使用lst
对象来修改计数器:
From there you can use the lst
object to modify your counter:
headIndex += lst.Count();
这篇关于表达式树可能不包含赋值运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!