I am new to linq. So far I’ve been primarily using linqTOsql like this: MyRepository.Mytable.Where(x => x.id == 2).
I am wondering how to take a list from that type of query and put a specific parameter into a new simple list like List<long>.
So I get my records like this:
List<Entry2Cats> e2c = genesisRepository.Entry2Cats.Where( x => x.streamEntryID == id).ToList()
And I want to get the category ID paramter for each item in e2c so I can move it into a List<long>.
Obvisouly, this can be done fairly simply with a foreach:
List<Entry2Cats> e2c = genesisRepository.Entry2Cats
.Where(x => x.streamEntryID == id)
.ToList();
List<long> e2cCatIDs = new List<long>();
foreach (Entry2Cats item in e2c)
{
e2cCatIDs.Add(item.catID);
}
Can this be done using just linq? If it can, should I even bother with it or is the foreach just as good?
Just use Select to get only the IDs.