G
Guest
I tried to add an one-up sequence to a LINQ select such as:
var i = 0;
var tokenized =
from line in lines
select new {
index = (i = i + 1)
,tokens = line.Split(new char[] {' '}
}
However, I get strange, but somewhat explainable results. It seems that
every time I access 'index' above the value increments. It is behaving as if
the code was assigned to the member and not the value. I have tried many
different ways to
reduce the result to a value without much success:
index = Convert.ToInt32((new int[] {i=i+1})[0].ToString())
in C# I defined a class for holding the values:
Tokenized
{
:
public int Index() { get {return _index; }}
:
}
and assigned _index using a static value at construction. And still saw the
same issue.
The objective was to assign values to each line so that they could be
refered to directly in later queries. But since the values keep changing,
this does not work.
This is a cool and interesting feature and I can think of many uses for
this, but it is irritating in that I can't seem to find a way to force it to
take the value rather than the expression.
Can someone tell me how to set up the equivalent of IDENTITY(1,1) in LINQ?
var i = 0;
var tokenized =
from line in lines
select new {
index = (i = i + 1)
,tokens = line.Split(new char[] {' '}
}
However, I get strange, but somewhat explainable results. It seems that
every time I access 'index' above the value increments. It is behaving as if
the code was assigned to the member and not the value. I have tried many
different ways to
reduce the result to a value without much success:
index = Convert.ToInt32((new int[] {i=i+1})[0].ToString())
in C# I defined a class for holding the values:
Tokenized
{
:
public int Index() { get {return _index; }}
:
}
and assigned _index using a static value at construction. And still saw the
same issue.
The objective was to assign values to each line so that they could be
refered to directly in later queries. But since the values keep changing,
this does not work.
This is a cool and interesting feature and I can think of many uses for
this, but it is irritating in that I can't seem to find a way to force it to
take the value rather than the expression.
Can someone tell me how to set up the equivalent of IDENTITY(1,1) in LINQ?