none
Using a Where as parameter? RRS feed

  • Question

  • Hello

    Given the following:
    1public IList<Data.Bob> All() 
    2        { 
    3            using (var dc = new DataClassesAddressLocationDataContext()) 
    4            { 
    5                var bobs = from g in dc.Bobs.Where(c => c.Location.XCoord == 55) 
    6                              select g; 
    7 
    8 
    9                return bobs.ToList(); 
    10            } 
    11        } 

    How do I do it using a Where Clause instead?

    Something similar like....
    1public IList<Data.Bob> All(Func<T, bool> query) 
    2        { 
    3            using (var dc = new DataClassesAddressLocationDataContext()) 
    4            { 
    5                var bobs = from g in dc.Bobs.Where(query) 
    6                              select g; 
    7 
    8 
    9                return bobs.ToList(); 
    10            } 
    11        } 

    Is it possible? And how is it called?


    Kind regards
    Janus

    Tuesday, February 24, 2009 1:52 PM

Answers

  • That is most definitely possible.  The way it would be called is like this:
    IList<Data.Bob> results = All(c => c.Location.XCoord == 55); 
    Also, you may want to change the generic parameter to just be Data.Bob since you aren't going to be using this method for anything else.


    Daniel - http://webs.neumont.edu/dstafford
    • Marked as answer by Janus007 Wednesday, February 25, 2009 8:17 PM
    Tuesday, February 24, 2009 4:04 PM

All replies

  • That is most definitely possible.  The way it would be called is like this:
    IList<Data.Bob> results = All(c => c.Location.XCoord == 55); 
    Also, you may want to change the generic parameter to just be Data.Bob since you aren't going to be using this method for anything else.


    Daniel - http://webs.neumont.edu/dstafford
    • Marked as answer by Janus007 Wednesday, February 25, 2009 8:17 PM
    Tuesday, February 24, 2009 4:04 PM
  • May be DynamicQuery is for you. You can find it under

    C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\CSharpSamples.zip

    ->LinqSamples ->DynamicQuery

    and some googling about "Linq DynamicQuery", you can find lots of samples too.
    Tuesday, February 24, 2009 7:27 PM
  • Thank you both

    Daniel you saved me alot of trouble :)
    Barbados, you inspired me to a more generic approach.


    Cheers
    Janus
    Wednesday, February 25, 2009 8:18 PM