-1

I trying to find a code of primitive type inside a List of objects and get the results into a new list.

I've been looking for something like that in Google but everyone talks about intersection between Lists and I want something like this.

            int AssocCode = 2;
            //Entity.ID
            //Entity.Name
            //Entity.AssociationCode
            List<Entity> list = new List<Entity>();
            list.Add(new Entity(1, "A", 1));
            list.Add(new Entity(2, "B", 2));
            list.Add(new Entity(3, "C", 3));
            list.Add(new Entity(4, "D", 2));
            list.Add(new Entity(5, "E", 2));

            /*I want the results for finding that code inside the collection**/
            List<Entity> newList = list .... find/intersect/select/remove/whatever (x => x.AssociationCode = AssocCode);
Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95

3 Answers3

4
int associationCode = 1;

List<Entity> newList = list.Where(e => e.AssociationCode == associationCode).ToList();

Worth mentioning that you should only call ToList() if you need the results right away.

This is better if you don't need the results right away.

IQueryable<Entity> entities = list.Where(e => e.AssociationCode == associationCode);

You can then filter further without hitting the database.

PeteGO
  • 5,597
  • 3
  • 39
  • 70
  • Ok PeteGO. I saw your modifications a little bit late. But can you tell me what's the difference between fullList = this.FullList.FindAll(item => item.AccCompanyID == CompanyCode); vs fullList = this.FullList.Where(item => item.AccCompanyID == CompanyCode).ToList(); Thank you for answering! – Maximus Decimus Jul 04 '13 at 18:18
  • Well as you have the list to begin with (everything loaded) there's actually not a lot of difference that I know of. The main thing is to try and only load what you need, when you need it, and using IQueryable and linq will do that for you. When you call .ToList() it fetches the items. – PeteGO Jul 04 '13 at 18:22
  • Thank you for you answer. I found more info about it here http://stackoverflow.com/questions/1938204/linq-where-vs-findall – Maximus Decimus Jul 04 '13 at 18:26
0

This should do it:

var newList = list.Where(e => e.AssociationCode == AssocCode).ToList()
NinjaNye
  • 7,046
  • 1
  • 32
  • 46
0

I solved it 1 minute ago my question. That works like this.

            List<Entity> newList =list.FindAll(item => item.AssociationCode == associationCode)

But now I see your answer PeteGO and I tested and it works also but at the end you new to add this .ToList()

Now I am a little bit confused. What is the different between these 2.

            fullList = this.FullList.FindAll(item => item.AccCompanyID == CompanyCode);
            vs
            fullList = this.FullList.Where(item => item.AccCompanyID == CompanyCode).ToList();
Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95
  • `Where` builds up the query and allows you to add additional contraints and actions such `GroupBy` and `OrderBy`. By calling `.ToList()` you are asking the context that you have finished building your query and that you want to retrieve the data into memory. – NinjaNye Jul 04 '13 at 18:20
  • Thank you! I also find on Internet this complementing info. http://stackoverflow.com/questions/1938204/linq-where-vs-findall – Maximus Decimus Jul 04 '13 at 18:24