linq vs lambda

C

CSharper

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("Person").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("Person")
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.
 
C

CSharper

[...]
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.Equals("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.
 
C

Chris Dunaway

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("Person").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("Person")
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("Person",
new XAttribute("id", "1"),
new XElement("Name", "a")),
new XElement("Person",
new XAttribute("id", "2"),
new XElement("Name", "b")));

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

var people1 = doc.Descendants().Elements("Person").Where(s
=> s.Attribute("id").Value.Equals("1"));

foreach (XElement element in people1) {
Console.WriteLine(element.ToString());
}

Console.WriteLine();

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

foreach (XElement element in people2) {
Console.WriteLine(element.ToString());
}

Console.WriteLine();

Console.WriteLine("Press ENTER to exit");
Console.ReadLine();
}


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top