Hello,
I'm new to LINQ,My knowledge on that library is from Jon Skeet's book "C# In Depth 1"
I read a lot of tutorials about LINQ in the past days including Skeet's book,but I could never find out a code snippet showing a normal C# code and then the shorter version of that code with LINQ so the reader can understand what does what.
My Problem: The function below opens a text file and searches for numbers placed at a specific place inside the text file.The numbers(I used ID in the code) start from 1 to 25000 and there are a few that are missing(for example there's no life 25,but 24,23,26,27 etc). I want the code to copy the lines in the array "Ids".Its not meant to be an array,I'm just new and I don't know if anything else would be more handy for LINQ.
public static IEnumerable<string> ReadLines(StreamReader reader)
{
while (!reader.EndOfStream)
{
yield return reader.ReadLine();
}
}
static void Filter(string filename)
{
using(var writer = File.CreateText(Application.StartupPath + "\\temp\\test.txt"))
{
using (var reader = File.OpenText(filename))
{
int[] Ids = { 14652, 14653, 14654, 14655, 14656, 14657, 14658, 14659, 14660 };
var myId = from id in Ids
from line in ReadLines(reader)
let items = line.Split('\t')
where items.Length > 1
let ItemId = int.Parse(items[1])
where ItemId == id
select line;
foreach (var id in myId)
{
writer.WriteLine(id);
}
}
}
}
What it writes: It only writes one line with number ,which is the first member of the Ids[] array(14652).
I had to add the second 'from' ,which is placed at first place in the code so it will check it for every member of the array.I get the from statement as a "while",because I couldn't find a snipper with normal code-> linq.
Where is the problem in the code?