IENumerable selecting indexes

Last post 11-11-2009 4:01 PM by Paul Linton. 5 replies.

Sort Posts:

  • IENumerable selecting indexes

    11-06-2009, 5:53 AM
    • Member
      2 point Member
    • Lars101
    • Member since 11-06-2009, 10:33 AM
    • Posts 8

    I have this code:


    IEnumerable<Product> productSelection = from product in products.... select product;


    where products.... I want to specify which indexes that are to be selected. Is it SelectMany that I should use? And if so, how would I specify that it's index 0-2 I want to select. Or how would you go about to solve my problem?

  • Re: IENumerable selecting indexes

    11-06-2009, 8:00 AM
    Answer
    • Contributor
      2,317 point Contributor
    • nisarkhan
    • Member since 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,361

    have you tried

     

    Element or ElementAt ? 

    Its all about coding!
    --------------------------
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: IENumerable selecting indexes

    11-06-2009, 7:15 PM
    Answer
    • Contributor
      5,076 point Contributor
    • Paul Linton
    • Member since 04-29-2008, 11:16 PM
    • Posts 883

     I think you need to use the alternate linq syntax (the correct name escapes me at the moment).  There is an overload of the Where operator which works with the index.  Something like

    IEnumerable<Product> productSelection  = products.Where((p,n)=> n <= 2);


     

    In this overload of the Where operator the 'p' is each product and 'n' is the index of the product.

    Hope that helps

    Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
  • Re: IENumerable selecting indexes

    11-11-2009, 7:50 AM
    Answer
    • Star
      14,312 point Star
    • JeffreyABecker
    • Member since 10-04-2004, 8:27 AM
    • Philadelphia, PA
    • Posts 2,918

    If you're trying to implement paging w/ linq to sql or entity framework use:

    var selection = products.Where(...).Skip(pageIndex * pageSize).Take(pageSize);


    The other two solutions will perform the full query and filter the indexes on the app-server where as linq to sql will emit sql to only bring back the results you want which will perform much better.

  • Re: IENumerable selecting indexes

    11-11-2009, 11:10 AM
    Answer
    • Member
      2 point Member
    • Lars101
    • Member since 11-06-2009, 10:33 AM
    • Posts 8
    I ended up using this code:


    // Specify which indexes to include
    int
    [] IndexesToInclude = new int[] {0,1,4,5,6};
    List<Product> filteredProducts = new List<Product>();
    foreach(int i in IndexesToInclude)
     
    if(i < products.Count()) filteredProducts.Add(products.ToArray()[i]);
  • Re: IENumerable selecting indexes

    11-11-2009, 4:01 PM
    Answer
    • Contributor
      5,076 point Contributor
    • Paul Linton
    • Member since 04-29-2008, 11:16 PM
    • Posts 883

    It would be a good idea to take the products.ToArray() out of the loop.  There is no need to do it every time you go around the loop.  Alternatively,

    int[] indexesToInclude = new int[] {0,1,4,5,6};
    IEnumerable<Product> filtered = products
                                                  .Where((p,n)=> indexesToInclude.Contains(n));


     

    Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
Page 1 of 1 (6 items)