linq vs lambda

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CSharper

    linq vs lambda

    I have following XML
    <root>
    <Person id="1">
    <Name>a</Name>
    </Person>
    <Person id="2">
    <Name>b</Name>
    </Person>
    </root>

    I am trying to find the person with id = 1 and I use the following

    var people =
    doc.Decendents( ).Elements("Per son").Where(s=> s.Attribute("id ").Value.Equals ("1"));

    When I run I get an empty list but when I change this to pure linq

    var people = from c in doc.Decendents( ).Elements("Per son")
    where c.Attributes(). Count() 0
    && c.Attribute("id ") != null
    && c.Attribute("id ").Value.Equals ("1")
    select c;

    This one does return me the first note.

    I have two questions here;

    Is it possible to include all these && conditions in the Lambda
    (stupid question) since most of all the examples I have seen are one
    liners. Also what is the difference between the lambda and linq in
    this selection that doesn't return the value? One side note, which is
    the good way to code, lambda or linq? I see the benefits of linq but
    for an untrained eye lambda could be confusing.

    Thanks.


  • Peter Duniho

    #2
    Re: linq vs lambda

    On Wed, 01 Oct 2008 10:32:25 -0700, CSharper <csharper@gmx.c omwrote:
    [...]
    I have two questions here;
    >
    Is it possible to include all these && conditions in the Lambda
    (stupid question) since most of all the examples I have seen are one
    liners.
    Sure. The lambda expression can be arbitrarily complex:

    s =s.Attributes() .Count() 0 && s.Attribute("id ") != null &&
    s.Attribute("id ").Value.Equals ("1")

    You can even use braces and declare local variables for efficiency (though
    this particular example is probably contrived, since the extra lookup of
    the attribute may not be that expensive):

    s ={ Attribute attr; return s.Attributes(). Count() 0 && (attr =
    s.Attribute("id ")) != null && attr.Value.Equa ls("1"); }
    Also what is the difference between the lambda and linq in
    this selection that doesn't return the value?
    That I'm not sure about. I would expect an exception to occur when you
    don't check for null, but it would surprise me that the Where() method
    would eat the exception. But then, maybe it does and if so maybe that's
    by design. I don't know enough about it.
    One side note, which is
    the good way to code, lambda or linq? I see the benefits of linq but
    for an untrained eye lambda could be confusing.
    That's three questions. :p

    I think the phrase "lambda or LINQ" is ill-conceived. The two are not
    mutually exclusive, and you are using LINQ in either example. In the
    first example, you're using the LINQ methods directly, while in the
    second, you're using the new C# LINQ syntax to automatically generate code
    to call the LINQ methods.

    But, if you're asking whether one syntax or the other is preferred of the
    two examples you provided, I'd say that's mostly a matter of preference,
    and partly a matter of utility. In particular, for the most part it's
    just what you think reads better, but I've run into situations where the
    compiler can't do delegate type inference unless you're using the methods
    directly, so in those cases it might make more sense to use the explicit
    LINQ methods rather than reworking whatever lambda expression one has to
    get the implicit LINQ syntax to work.

    Pete

    Comment

    • CSharper

      #3
      Re: linq vs lambda

      On Oct 1, 1:08 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
      wrote:
      On Wed, 01 Oct 2008 10:32:25 -0700, CSharper <cshar...@gmx.c omwrote:
      [...]
      I have two questions here;
      >
      Is it possible to include all these && conditions in the Lambda
      (stupid question) since most of all the examples I have seen are one
      liners.
      >
      Sure.  The lambda expression can be arbitrarily complex:
      >
           s =s.Attributes() .Count() 0 && s.Attribute("id ") != null &&  
      s.Attribute("id ").Value.Equals ("1")
      >
      You can even use braces and declare local variables for efficiency (though  
      this particular example is probably contrived, since the extra lookup of  
      the attribute may not be that expensive):
      >
           s ={ Attribute attr; return s.Attributes(). Count() 0 && (attr =  
      s.Attribute("id ")) != null && attr.Value.Equa ls("1"); }
      >
      Also what is the difference between the lambda and linq in
      this selection that doesn't return the value?
      >
      That I'm not sure about.  I would expect an exception to occur when you 
      don't check for null, but it would surprise me that the Where() method  
      would eat the exception.  But then, maybe it does and if so maybe that's  
      by design.  I don't know enough about it.
      >
      One side note, which is
      the good way to code, lambda or linq? I see the benefits of linq but
      for an untrained eye lambda could be confusing.
      >
      That's three questions.  :p
      >
      I think the phrase "lambda or LINQ" is ill-conceived.  The two are not  
      mutually exclusive, and you are using LINQ in either example.  In the  
      first example, you're using the LINQ methods directly, while in the  
      second, you're using the new C# LINQ syntax to automatically generate code  
      to call the LINQ methods.
      >
      But, if you're asking whether one syntax or the other is preferred of the 
      two examples you provided, I'd say that's mostly a matter of preference,  
      and partly a matter of utility.  In particular, for the most part it's  
      just what you think reads better, but I've run into situations where the  
      compiler can't do delegate type inference unless you're using the methods 
      directly, so in those cases it might make more sense to use the explicit  
      LINQ methods rather than reworking whatever lambda expression one has to  
      get the implicit LINQ syntax to work.
      >
      Pete
      Hi Pete,

      Thank you for the answer (I sneaked third question in and you cought
      me on that). By any chance why the first query doesn't return the
      value while the second one does?

      Thanks.

      Comment

      • Peter Duniho

        #4
        Re: linq vs lambda

        On Wed, 01 Oct 2008 11:21:01 -0700, CSharper <csharper@gmx.c omwrote:
        Thank you for the answer (I sneaked third question in and you cought
        me on that). By any chance why the first query doesn't return the
        value while the second one does?
        You had three questions. I had three answers. It looks like you skipped
        over one. :)

        Comment

        • Chris Dunaway

          #5
          Re: linq vs lambda

          On Oct 1, 12:32 pm, CSharper <cshar...@gmx.c omwrote:
          I have following XML
          <root>
          <Person id="1">
          <Name>a</Name>
          </Person>
          <Person id="2">
          <Name>b</Name>
          </Person>
          </root>
          >
          I am trying to find the person with id = 1 and I use the following
          >
          var people =
          doc.Decendents( ).Elements("Per son").Where(s=> s.Attribute("id ").Value.Equals ("1"));
          >
          When I run I get an empty list but when I change this to pure linq
          >
          var people = from c in doc.Decendents( ).Elements("Per son")
          where c.Attributes(). Count() 0
          && c.Attribute("id ") != null
          && c.Attribute("id ").Value.Equals ("1")
          select c;
          >
          This one does return me the first note.
          >
          I have two questions here;
          >
          Is it possible to include all these && conditions in the Lambda
          (stupid question) since most of all the examples I have seen are one
          liners. Also what is the difference between the lambda and linq in
          this selection that doesn't return the value? One side note, which is
          the good way to code, lambda or linq? I see the benefits of linq but
          for an untrained eye lambda could be confusing.
          >
          Thanks.
          That can't be your actual code, because there is no method called
          "Decendents " in the XDocument class. That must be a typo.

          In any event, I ran the following code:

          static void Main(string[] args) {

          var xml = new XElement("root" ,
          new XElement("Perso n",
          new XAttribute("id" , "1"),
          new XElement("Name" , "a")),
          new XElement("Perso n",
          new XAttribute("id" , "2"),
          new XElement("Name" , "b")));

          XDocument doc = new XDocument();
          doc.Add(xml);

          var people1 = doc.Descendants ().Elements("Pe rson").Where(s
          =s.Attribute("i d").Value.Equal s("1"));

          foreach (XElement element in people1) {
          Console.WriteLi ne(element.ToSt ring());
          }

          Console.WriteLi ne();

          var people2 = from c in
          doc.Descendants ().Elements("Pe rson")
          where c.Attributes(). Count() 0
          && c.Attribute("id ") != null
          && c.Attribute("id ").Value.Equals ("1")
          select c;

          foreach (XElement element in people2) {
          Console.WriteLi ne(element.ToSt ring());
          }

          Console.WriteLi ne();

          Console.WriteLi ne("Press ENTER to exit");
          Console.ReadLin e();
          }


          With the following results:

          <Person id="1">
          <Name>a</Name>
          </Person>

          <Person id="1">
          <Name>a</Name>
          </Person>

          Press ENTER to exit

          So it appears to work correctly to me. Either you've got a typo
          somewhere in your code or your xml does not match what you are showing
          us.

          Can you supply a small, but complete code example that duplicates the
          problem?

          Chris

          Chris

          Comment

          Working...