constains or linq

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?cmF1bGF2aQ==?=

    constains or linq

    linq on objects...

    want to find if the object exist in its collection


    if I have a loop searching in the collection
    foreach (myClass r in collections)
    {
    if (r.field01 == type && r.field02 == id)
    return true;
    }
    return false;

    versus
    Linq
    List <myClassqq =
    (from r in SecondaryIds
    where r.field01 == type && t.field02 == id
    select t).ToList();


    1. any performance issues. (lets say there are many obj in collection)
    2. how do get collection count from linq or can I set a true variable if
    found?

    thanks


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: constains or linq

    raulavi,

    I would think so. In the first, you are just checking for the existence
    of one object that satisfies the condition. In the second, you are getting
    all of the objects that satisfy the condition. What you really want to do
    is the following:

    bool matches = SecondaryIds.An y(r =r.field01 = type && r.field02 == id);

    That will give you the same result as the first (assuming the
    collections are the same, as the code you use doesn't indicate that they
    are).


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "raulavi" <raulavi@discus sions.microsoft .comwrote in message
    news:99BFD4B6-A282-4CDF-9B7D-3C07E833E516@mi crosoft.com...
    linq on objects...
    >
    want to find if the object exist in its collection
    >
    >
    if I have a loop searching in the collection
    foreach (myClass r in collections)
    {
    if (r.field01 == type && r.field02 == id)
    return true;
    }
    return false;
    >
    versus
    Linq
    List <myClassqq =
    (from r in SecondaryIds
    where r.field01 == type && t.field02 == id
    select t).ToList();
    >
    >
    1. any performance issues. (lets say there are many obj in collection)
    2. how do get collection count from linq or can I set a true variable if
    found?
    >
    thanks
    >
    >

    Comment

    • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

      #3
      Re: constains or linq

      raulavi wrote:
      linq on objects...
      >
      want to find if the object exist in its collection
      >
      >
      if I have a loop searching in the collection
      foreach (myClass r in collections)
      {
      if (r.field01 == type && r.field02 == id)
      return true;
      }
      return false;
      >
      versus
      Linq
      List <myClassqq =
      (from r in SecondaryIds
      where r.field01 == type && t.field02 == id
      select t).ToList();
      Neither.
      >
      >
      1. any performance issues. (lets say there are many obj in collection)
      Yes, of course. You are looping the collection, that doesn't scale well.
      2. how do get collection count from linq or can I set a true variable if
      found?
      >
      thanks
      >
      If you want to find the item quickly, you should use a Dictionary. The
      ContainsKey method is close to O(1), meaning it's almost as fast
      regardless of the number of items in the dictionary.

      A Dictionary uses a hash code for the key, so you would need a key type
      that contains the two fields that you check for, and a way to calculate
      a hash code for that.

      Incidentally, I wrote an article about using a Dictionary with a custom
      key a while back:




      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • =?Utf-8?B?cmF1bGF2aQ==?=

        #4
        Re: constains or linq

        thanks, they are and you are right....
        Your sample is much better and that's exactly what I want.

        I also wanted to know performance wise.

        the only problem I may expect is if my sample of linq is ran on a very large
        database, it might ran out of memeory, correct? then , if so, how do you trap
        the error, try/catch and where can i read about what exceptions might throw?




        "Nicholas Paldino [.NET/C# MVP]" wrote:
        raulavi,
        >
        I would think so. In the first, you are just checking for the existence
        of one object that satisfies the condition. In the second, you are getting
        all of the objects that satisfy the condition. What you really want to do
        is the following:
        >
        bool matches = SecondaryIds.An y(r =r.field01 = type && r.field02 == id);
        >
        That will give you the same result as the first (assuming the
        collections are the same, as the code you use doesn't indicate that they
        are).
        >
        >
        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m
        >
        "raulavi" <raulavi@discus sions.microsoft .comwrote in message
        news:99BFD4B6-A282-4CDF-9B7D-3C07E833E516@mi crosoft.com...
        linq on objects...

        want to find if the object exist in its collection


        if I have a loop searching in the collection
        foreach (myClass r in collections)
        {
        if (r.field01 == type && r.field02 == id)
        return true;
        }
        return false;

        versus
        Linq
        List <myClassqq =
        (from r in SecondaryIds
        where r.field01 == type && t.field02 == id
        select t).ToList();


        1. any performance issues. (lets say there are many obj in collection)
        2. how do get collection count from linq or can I set a true variable if
        found?

        thanks
        >
        >
        >

        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: constains or linq

          raulavi,

          Well, it depends. If this is a LINQ to SQL query, then the query is
          composed and sent to the server, and you don't have to worry about out of
          memory exceptions (generally speaking).

          With this particular query, you don't have deferred execution (the Any
          method returns a boolean, not an IEnumerable<Twh ich can be deferred) so it
          will be executed on that line of code. If you are going to wrap anything in
          a try/catch block, it's that line.

          If this is all in memory, then you shouldn't run across exceptions due
          to lack of memory, because it is assumed that the collection is already in
          memory (unless you are streaming it through an IEnumerable<Tim plementation
          and the results are being created on the fly, which I don't think is the
          case).

          As far as performance, the Any method is going to finish terminating the
          moment it runs across a condition that is true, just like your loop, so I
          expect the performance characteristics to be the same.


          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m

          "raulavi" <raulavi@discus sions.microsoft .comwrote in message
          news:5269D5C7-6B7B-41A8-BC21-1E94A5E9AB2D@mi crosoft.com...
          thanks, they are and you are right....
          Your sample is much better and that's exactly what I want.
          >
          I also wanted to know performance wise.
          >
          the only problem I may expect is if my sample of linq is ran on a very
          large
          database, it might ran out of memeory, correct? then , if so, how do you
          trap
          the error, try/catch and where can i read about what exceptions might
          throw?
          >
          >
          >
          >
          "Nicholas Paldino [.NET/C# MVP]" wrote:
          >
          >raulavi,
          >>
          > I would think so. In the first, you are just checking for the
          >existence
          >of one object that satisfies the condition. In the second, you are
          >getting
          >all of the objects that satisfy the condition. What you really want to
          >do
          >is the following:
          >>
          >bool matches = SecondaryIds.An y(r =r.field01 = type && r.field02 ==
          >id);
          >>
          > That will give you the same result as the first (assuming the
          >collections are the same, as the code you use doesn't indicate that they
          >are).
          >>
          >>
          >--
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >>
          >"raulavi" <raulavi@discus sions.microsoft .comwrote in message
          >news:99BFD4B 6-A282-4CDF-9B7D-3C07E833E516@mi crosoft.com...
          linq on objects...
          >
          want to find if the object exist in its collection
          >
          >
          if I have a loop searching in the collection
          foreach (myClass r in collections)
          {
          if (r.field01 == type && r.field02 == id)
          return true;
          }
          return false;
          >
          versus
          Linq
          List <myClassqq =
          (from r in SecondaryIds
          where r.field01 == type && t.field02 == id
          select t).ToList();
          >
          >
          1. any performance issues. (lets say there are many obj in collection)
          2. how do get collection count from linq or can I set a true variable
          if
          found?
          >
          thanks
          >
          >
          >>
          >>
          >>

          Comment

          • =?Utf-8?B?cmF1bGF2aQ==?=

            #6
            Re: constains or linq

            greate! very good explanation...r ight on the nail Nicholas,thanks

            "Nicholas Paldino [.NET/C# MVP]" wrote:
            raulavi,
            >
            Well, it depends. If this is a LINQ to SQL query, then the query is
            composed and sent to the server, and you don't have to worry about out of
            memory exceptions (generally speaking).
            >
            With this particular query, you don't have deferred execution (the Any
            method returns a boolean, not an IEnumerable<Twh ich can be deferred) so it
            will be executed on that line of code. If you are going to wrap anything in
            a try/catch block, it's that line.
            >
            If this is all in memory, then you shouldn't run across exceptions due
            to lack of memory, because it is assumed that the collection is already in
            memory (unless you are streaming it through an IEnumerable<Tim plementation
            and the results are being created on the fly, which I don't think is the
            case).
            >
            As far as performance, the Any method is going to finish terminating the
            moment it runs across a condition that is true, just like your loop, so I
            expect the performance characteristics to be the same.
            >
            >
            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m
            >
            "raulavi" <raulavi@discus sions.microsoft .comwrote in message
            news:5269D5C7-6B7B-41A8-BC21-1E94A5E9AB2D@mi crosoft.com...
            thanks, they are and you are right....
            Your sample is much better and that's exactly what I want.

            I also wanted to know performance wise.

            the only problem I may expect is if my sample of linq is ran on a very
            large
            database, it might ran out of memeory, correct? then , if so, how do you
            trap
            the error, try/catch and where can i read about what exceptions might
            throw?




            "Nicholas Paldino [.NET/C# MVP]" wrote:
            raulavi,
            >
            I would think so. In the first, you are just checking for the
            existence
            of one object that satisfies the condition. In the second, you are
            getting
            all of the objects that satisfy the condition. What you really want to
            do
            is the following:
            >
            bool matches = SecondaryIds.An y(r =r.field01 = type && r.field02 ==
            id);
            >
            That will give you the same result as the first (assuming the
            collections are the same, as the code you use doesn't indicate that they
            are).
            >
            >
            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m
            >
            "raulavi" <raulavi@discus sions.microsoft .comwrote in message
            news:99BFD4B6-A282-4CDF-9B7D-3C07E833E516@mi crosoft.com...
            linq on objects...

            want to find if the object exist in its collection


            if I have a loop searching in the collection
            foreach (myClass r in collections)
            {
            if (r.field01 == type && r.field02 == id)
            return true;
            }
            return false;

            versus
            Linq
            List <myClassqq =
            (from r in SecondaryIds
            where r.field01 == type && t.field02 == id
            select t).ToList();


            1. any performance issues. (lets say there are many obj in collection)
            2. how do get collection count from linq or can I set a true variable
            if
            found?

            thanks


            >
            >
            >
            >

            Comment

            Working...