Find sub-list from a generic list

Discussion in 'Microsoft Dot NET' started by Curious, Mar 11, 2010.

  1. Curious

    Curious Guest

    Given a generic list of items, when each item has some attributes such
    as name, age, etc.

    How to get a sublist of the items filtered by an attribute, (say age =
    40)?

    I can only think of looping through each item on the list, and check
    to see if its age is 40. Is there more efficient way(s) to do this?
     
    Curious, Mar 11, 2010
    #1
    1. Advertisements

  2. Curious

    Mr. Arnold Guest

    Curious wrote:
    > Given a generic list of items, when each item has some attributes such
    > as name, age, etc.
    >
    > How to get a sublist of the items filtered by an attribute, (say age =
    > 40)?
    >
    > I can only think of looping through each item on the list, and check
    > to see if its age is 40. Is there more efficient way(s) to do this?


    It's called LINQ.

    var thelist = (from a in list.where(a => a.age == 40)select a).tolist();

    http://en.wikipedia.org/wiki/Language_Integrated_Query
     
    Mr. Arnold, Mar 11, 2010
    #2
    1. Advertisements

  3. Curious

    gerry Guest

    that's a pretty convoluted mixing of metaphors , to put it much simpler :

    var thelist = list.Where(a => a.age == 40);

    or

    var = from a in list where a.age==40 select a;


    "Mr. Arnold" <> wrote in message
    news:...
    > Curious wrote:
    >> Given a generic list of items, when each item has some attributes such
    >> as name, age, etc.
    >>
    >> How to get a sublist of the items filtered by an attribute, (say age =
    >> 40)?
    >>
    >> I can only think of looping through each item on the list, and check
    >> to see if its age is 40. Is there more efficient way(s) to do this?

    >
    > It's called LINQ.
    >
    > var thelist = (from a in list.where(a => a.age == 40)select a).tolist();
    >
    > http://en.wikipedia.org/wiki/Language_Integrated_Query
     
    gerry, Mar 11, 2010
    #3
  4. Curious

    Mr. Arnold Guest

    gerry wrote:
    > that's a pretty convoluted mixing of metaphors , to put it much simpler :
    >
    > var thelist = list.Where(a => a.age == 40);
    >
    > or
    >
    > var = from a in list where a.age==40 select a;


    I don't know what you're talking about. And I don't think you know what
    you're talking about.

    Do you know anything about this?

    http://msdn.microsoft.com/en-us/library/bb397687.aspx

    (var =) I don't think that's going to work anywhere but giving a compile
    error.

    var thelist = (from a in list.where(a => a.age == 40)select a)tolist();

    var list = (from a in accounts1.OrderBy(b => b.Lastname).Where(c =>
    c.Age == 40)).tolist();

    Do you not see the power of the expression?

    You should have been posting to the OP and not me.

    The next time you get in someone's face with a bunch of nonsense you had
    better be correct yourself.
     
    Mr. Arnold, Mar 12, 2010
    #4
  5. Curious

    gerry Guest

    "Mr. Arnold" <> wrote in message
    news:...
    > gerry wrote:
    >> that's a pretty convoluted mixing of metaphors , to put it much simpler :
    >>
    >> var thelist = list.Where(a => a.age == 40);
    >>
    >> or
    >>
    >> var = from a in list where a.age==40 select a;

    >
    > I don't know what you're talking about. And I don't think you know what
    > you're talking about.


    huh - other than the 'var =' typo - what's the problem ?

    >
    > Do you know anything about this?
    >
    > http://msdn.microsoft.com/en-us/library/bb397687.aspx
    >


    sure I use it all the time

    > (var =) I don't think that's going to work anywhere but giving a compile
    > error.


    if we are going to get typographically picky here - did you try to compile
    the original example you gave ? Even the new one added below here won't
    compile as is.

    >
    > var thelist = (from a in list.where(a => a.age == 40)select a)tolist();
    >
    > var list = (from a in accounts1.OrderBy(b => b.Lastname).Where(c => c.Age
    > == 40)).tolist();
    >
    > Do you not see the power of the expression?


    Sure I do - I also see the unnecessary complexity, jumble and
    innefficiency - do you ?
    Personally I try not to mix linq query syntax with & lambda expressions
    whenever possible - KISS.

    In your original example : ( note the corrections - the original code would
    not compile ! )
    var thelist = (from a in list.Where(a => a.age == 40)select a).ToList();
    the segment
    (from a in list.Where(a => a.age == 40)select a
    is functionally equivalent to both
    from a in list where a.age == 40 select a
    and
    list.Where(a => a.age == 40)
    Depending on the usage .Tolist() could be unnecessary, if for some reason
    you really need it then
    list.Where(a => a.age == 40).ToList()
    will do it, by responding that either
    var thelist = list.Where(a => a.age == 40);
    or
    var thelist = from a list where age == 40 select a;
    are both simpler ( less convoluted ) than
    var thelist = (from a in list.Where(a => a.age == 40)select a)ToList();
    is just stating the obvious

    similarly with the 2nd newly added example , either
    var list = from a in accounts1 where a.Age == 40 orderby a.Lastname
    select a;
    or
    var list = accounts1.Where(a => a.Age == 40).OrderBy(a => a.Lastname);
    are both perferable to
    var list = (from a in accounts1.OrderBy(b => b.Lastname).Where(c =>
    c.Age == 40)).tolist();
    both in readablilty and efficiency.


    >
    > You should have been posting to the OP and not me.
    >


    I was replying to the thread, not to you.

    > The next time you get in someone's face with a bunch of nonsense you had
    > better be correct yourself.


    in whose face how and with what nonsense ?
    as noted above - your examples were/are incorrect as well.

    just out of curiosity - why did you change your original link from
    http://en.wikipedia.org/wiki/Language_Integrated_Query to
    http://msdn.microsoft.com/en-us/library/bb397687.aspx
    and add the second linq example as if it was part of your original response
    ?
     
    gerry, Mar 12, 2010
    #5
  6. Curious

    Mr. Arnold Guest

    gerry wrote:
    > "Mr. Arnold" <> wrote in message
    > news:...

    ssion?
    >
    > Sure I do - I also see the unnecessary complexity, jumble and
    > innefficiency - do you ?



    > Personally I try not to mix linq query syntax with & lambda expressions
    > whenever possible - KISS.


    <yawn>

    Opinions are a dime a dozen and everyone has an opinion including you.

    You got out line with your smart mouth out the gate.

    You should have posted to the OP and not me, as I could care less about
    what's on your mind.

    You need to stay out of my sight. Does anything else need to be said here?
     
    Mr. Arnold, Mar 12, 2010
    #6
  7. Curious

    gerry Guest

    lol - come back when you get out of your teens

    "Mr. Arnold" <> wrote in message
    news:...
    > gerry wrote:
    >> "Mr. Arnold" <> wrote in message
    >> news:...

    > ssion?
    >>
    >> Sure I do - I also see the unnecessary complexity, jumble and
    >> innefficiency - do you ?

    >
    >
    >> Personally I try not to mix linq query syntax with & lambda expressions
    >> whenever possible - KISS.

    >
    > <yawn>
    >
    > Opinions are a dime a dozen and everyone has an opinion including you.
    >
    > You got out line with your smart mouth out the gate.
    >
    > You should have posted to the OP and not me, as I could care less about
    > what's on your mind.
    >
    > You need to stay out of my sight. Does anything else need to be said here?
     
    gerry, Mar 17, 2010
    #7
  8. Curious

    Mr. Arnold Guest

    gerry wrote:
    > lol - come back when you get out of your teens
    >


    You get out of your baby years.

    Why don't you stop trolling me boy? You are of no importance to me. So,
    go play in heavy street traffic and get ran over.
     
    Mr. Arnold, Mar 17, 2010
    #8
  9. Curious

    gerry Guest

    I am truly sorry, obviously you have some issues.


    "Mr. Arnold" <> wrote in message
    news:%...
    > gerry wrote:
    >> lol - come back when you get out of your teens
    >>

    >
    > You get out of your baby years.
    >
    > Why don't you stop trolling me boy? You are of no importance to me. So, go
    > play in heavy street traffic and get ran over.
     
    gerry, Mar 17, 2010
    #9
  10. Curious

    Mr. Arnold Guest

    gerry wrote:
    > I am truly sorry, obviously you have some issues.


    Yeah, you are sorry that's for sure. And the issue is with you boy
    running your mouth and getting in people's face with your take on things.

    Who cares as to what you think? You are nobody.

    Now, I am going <plank> you boy that's a soft logical <plonk>, because I
    have to eyeball a *clown* like you.

    <bye>
     
    Mr. Arnold, Mar 17, 2010
    #10
  11. Curious

    gerry Guest

    Once again, I am truly sorry for pointing out the short comings of a very
    poor answer provided by someone who's immense knowledge of this subject was
    derived from an online wiki.
    If you are going to post such confused drivel in response to questions on
    subjects on which you have very little or at least a very confused
    understanding, don't crap on the person that calls you out.

    This has been fun Mr. T ( really ) but now I like many others put you on
    ignore, because I really don't care.



    "Mr. Arnold" <> wrote in message
    news:...
    > gerry wrote:
    >> I am truly sorry, obviously you have some issues.

    >
    > Yeah, you are sorry that's for sure. And the issue is with you boy running
    > your mouth and getting in people's face with your take on things.
    >
    > Who cares as to what you think? You are nobody.
    >
    > Now, I am going <plank> you boy that's a soft logical <plonk>, because I
    > have to eyeball a *clown* like you.
    >
    > <bye>
    >
     
    gerry, Mar 18, 2010
    #11
  12. Curious

    Mr. Arnold Guest

    gerry wrote:

    <snipped>
    <not even reading your lip service>

    I see you escaped out of your crib again, and you're on the loose. You
    go harass someone else little boy.
     
    Mr. Arnold, Mar 18, 2010
    #12
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    Problem calling a sub from a sub

    Guest, Nov 24, 2004, in forum: Microsoft Dot NET
    Replies:
    3
    Views:
    163
    Guest
    Nov 25, 2004
  2. Guest
    Replies:
    1
    Views:
    408
    Guest
    May 5, 2006
  3. Guest

    Sorting a generic list by letter and number

    Guest, Jun 12, 2006, in forum: Microsoft Dot NET
    Replies:
    3
    Views:
    185
    Dan Manges
    Jun 12, 2006
  4. Carl Daniel [VC++ MVP]

    Re: Problem with generic List<>

    Carl Daniel [VC++ MVP], Nov 17, 2006, in forum: Microsoft Dot NET
    Replies:
    1
    Views:
    201
    ThunderMusic
    Nov 17, 2006
  5. DR
    Replies:
    1
    Views:
    274
    Peter Ritchie [C# MVP]
    Apr 9, 2008
Loading...

Share This Page