user1069733
user1069733

Reputation: 485

LINQ query with variable in where clause

I have been at this for hours, poured over multiple questions that are similar. What do I do to get this code to work?

public void DoSomething(List<TheObj> objs, string lnameStr)
    {
        var pQuery = (from o in objs
                      where o.Lname.Contains(lnameStr)
                      select o).ToList();

        foreach (var theObj in pQuery)
        {
            Trace.WriteLine(theObj.Fname);
        }
    }

Thanks

Upvotes: 1

Views: 1992

Answers (1)

Amen Ayach
Amen Ayach

Reputation: 4348

public void DoSomething(List<TheObj> objs, string lnameStr)
    {
      if(objs != null && !string.IsNullOrEmpty(lnameStr))   
       {
        var pQuery = (from o in objs
                      where !string.IsNullOrEmpty(p.Lname) && 
                            o.Lname.Contains(lnameStr)
                      select o).ToList();

        foreach (var theObj in pQuery)
        {
            Trace.WriteLine(theObj.Fname);
        }
      }
    }

Upvotes: 2

Related Questions