none
LINQ - what's the difference between select() and where()? RRS feed

Answers

  •  

    Hi David,

    Welcome!

    Select() can maps an IEnumerable to a new structure.

    public static IEnumerable<TResult> Select<TSource, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, TResult> selector
    )// the return type is same with the Result type

    A.Select(p=>new B{ID=p.id, Name=p.name})// IEnumerable<A> in-------> IEnumerable<B> out
    Where() filter the  IEnumerable and return the subset of the original  IEnumerable

    A.Where(P=>p.id>0) // IEnumerable<A> in-------->  IEnumerable<A> out

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, May 17, 2011 7:57 AM
    Moderator

All replies

  • "select" is used to provide the collection of item, while "where" is used to mark a condition on these item

    it's similar to a sql query

    SELECT (item from collection) WHERE (ITEM name starts with A)

    so if u drop "WHERE" clause it ll return all the items from collection, With where cluase it ll return only items which satisfies the condtion

     

     


    Manish Sati
    Monday, May 16, 2011 5:20 AM
  • Hi Manish;

    But can't I also use a Select() to determine which elements to return from my IEnumerable?

    thanks - dave


    The future of B.I. - Raw & Uncut
    Monday, May 16, 2011 1:59 PM
  •  

    Hi David,

    Welcome!

    Select() can maps an IEnumerable to a new structure.

    public static IEnumerable<TResult> Select<TSource, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, TResult> selector
    )// the return type is same with the Result type

    A.Select(p=>new B{ID=p.id, Name=p.name})// IEnumerable<A> in-------> IEnumerable<B> out
    Where() filter the  IEnumerable and return the subset of the original  IEnumerable

    A.Where(P=>p.id>0) // IEnumerable<A> in-------->  IEnumerable<A> out

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, May 17, 2011 7:57 AM
    Moderator
  • Thank you. The "to a new structure" is what made the difference clear for me.
    The future of B.I. - Raw & Uncut
    Tuesday, May 17, 2011 12:00 PM
  • You are welcome!

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 18, 2011 1:17 AM
    Moderator