An expression tree may not contain an assignment operator?

How can I increase the index value in linq expression.

  int headIndex = -1;
           // int itemIndex = -1;
            lst = (from xx in db.vwCustomizationHeaders
                   where xx.ProductID == pID
                   select new custHeader()
                   {
                       headIndex = headIndex++,// Guid.NewGuid(),
             }
+4
source share
1 answer

While you create this request in the code:

from xx in db.vwCustomizationHeaders
where xx.ProductID == pID
select new custHeader()
{
    headIndex = headIndex++
}

It actually runs in the database. And the database cannot change the values ​​in your code. Therefore, you cannot increase this local code value ( headIndex) from the database. (Also, as @Kirk Woll pointed out, it’s a very bad practice to change such values ​​in the selection. The choice is to simply extract / create something and not change the state or create side effects.)

, , , select. :

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();
+4

Source: https://habr.com/ru/post/1529555/


All Articles