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(),
}
While you're creating this query in code:
from xx in db.vwCustomizationHeaders
where xx.ProductID == pID
select new custHeader()
{
headIndex = headIndex++
}
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.)
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);
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.
});
From there you can use the lst
object to modify your counter:
headIndex += lst.Count();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With