表达式树可能不包含赋值运算符? [英] An expression tree may not contain an assignment operator?

查看:354
本文介绍了表达式树可能不包含赋值运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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 vwCustomizationHeaders 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆