none
LINQ multiple where clause RRS feed

  • Question

  • Hello all...thicky MrDean here!!!

    I have the following code that build a web method that is ultimately used in the good old autocompleteextender

    TORDataContext db = new TORDataContext();
    
                return db.tblReporting
    
                    .Where(r => r.UnitUserfield1.Contains(prefixText)).OrderBy(r => r.UnitUserfield1).Select(r => r.UnitUserfield1).Take(count).Distinct().ToArray();
    Now, I would like to add in another where clause into this i.e. to select a certain member within the tblReporting. I have attempted the following with no success:

    return db.tblReporting
    
                    .Where(r => r.MemberId=("123") &&(r => r.UnitUserfield1.Contains(prefixText)).OrderBy(r => r.UnitUserfield1).Select(r => r.UnitUserfield1).Take(count).Distinct().ToArray();
    Can someone point out the error in my ways - apologies for what is a simple question, I am brand spanking new to this development malarky!
    Tuesday, November 17, 2009 1:16 PM

Answers


  • it does work if you remove r=> from the second condition .. below modified statement

    return db.tblReporting
    .Where(r => r.MemberId=("123") &&(r.UnitUserfield1.Contains(prefixText)).OrderBy(r => r.UnitUserfield1).Select(r => r.UnitUserfield1).Take(count).Distinct().ToArray();


    Hope this works

    Thanks
    Jbasingh
    • Marked as answer by MrRichDean Tuesday, November 17, 2009 3:43 PM
    Tuesday, November 17, 2009 2:26 PM

All replies

  • Hi MrDean,

    Your syntax for adding the member ID as a where clause filter looks correct. Can you try using some of Damien Guard's tips here to find what SQL is sent to the database? This should help you diagnose what the failure is.

    Hope that helps,
    David

    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    Tuesday, November 17, 2009 1:37 PM
    Answerer

  • it does work if you remove r=> from the second condition .. below modified statement

    return db.tblReporting
    .Where(r => r.MemberId=("123") &&(r.UnitUserfield1.Contains(prefixText)).OrderBy(r => r.UnitUserfield1).Select(r => r.UnitUserfield1).Take(count).Distinct().ToArray();


    Hope this works

    Thanks
    Jbasingh
    • Marked as answer by MrRichDean Tuesday, November 17, 2009 3:43 PM
    Tuesday, November 17, 2009 2:26 PM
  • Thanks for this - yes by simply removing the second r=>, the desired result was achieved.

    All that head scratching for three characters!!!

    Thanks for the help and the tips.
    Tuesday, November 17, 2009 3:44 PM