Quote:
So, how I can make to get 2 by 2 lines with linq ?
I strongly urge you to stay with your non-LINQ code as it will be much easier to understand and change (if needed) later on. Do however, make the changes that Bill Woodruff has suggested.
With that said, it can be fun to see what you can force LINQ to achieve :0 even if the resulting code is a nightmare to read and maintain:~.
Here are two ways that came to mind to achieve this in LINQ.
string[] lines = {"1 abc","2 abc","7 abc","7 efd","4 mno","1 xyz","7 nope"};
string[] filteredlines = lines.Where((string x) => x.StartsWith("1") || x.StartsWith("7")).ToArray();
List<test> BadIdea;
BadIdea = filteredlines.Where((string dummy, Int32 index) => (((index & 1) == 0) && (index <= (filteredlines.Length / 2)) && (filteredlines.Length > 1))).
Select((string dummy, Int32 index) =>
new Test {Line1 = filteredlines[index * 2], Line2 = filteredlines[(index * 2) + 1]}).ToList();
List<test> ReallyBadIdea = default(List<test>);
ReallyBadIdea = (List<test>)(from dummyvar in new Int32[] {1}
let tmpFilteredLines = lines.Where((string x) => x.StartsWith("1") || x.StartsWith("7")).ToArray()
select (tmpFilteredLines.Where((string dummy, Int32 index) =>
(((index & 1) == 0) && (index <= (tmpFilteredLines.Length / 2)) && (tmpFilteredLines.Length > 1))).
Select((string dummy, Int32 index) => new Test() { Line1 = tmpFilteredLines[index * 2], Line2 = tmpFilteredLines[(index * 2) + 1] }).ToList())).ToList()[0];
</test></test></test></test>