Lambda multiple conditions to select objects from List<T>

16,146

Solution 1

You can simply use the normal || operator:

workResults = workResults.Where((user => 
                  user.work.Any(wrk => wrk.employer.name == "something" || 
                                       wrk.position.name == "something")
                               ));

Solution 2

Well, you could just use C#s or operator (||).

Share:
16,146
ThdK
Author by

ThdK

Web developer and photographer

Updated on June 04, 2022

Comments

  • ThdK
    ThdK almost 2 years

    I 'm using c# to select some objects in a List. The following code is working.

    public void filterByWork(string work, int precision)
            {
                workResults = new List<FbUser>();
                Array keywords = work.Split(' ');
                workResults = userlist.Where(user => user.work != null);
                workResults = workResults.Where((user => user.work.Any(wrk => StringExtensions.match(wrk.employer.name, keywords) >= precision)));
    
            }
    

    But what if i want more than one condition? Can i use the 'OR' keyword somewhere? Because i want to select all objects where the wrk.employer.name = "something" OR wrk.position.name = "something". How can do this?

    Thanks in advance!

  • sehe
    sehe about 13 years
    too true. however, since this is SO (where actually helping regardless of question is high in regard) my vote is with the answer that shows an example on this one