473,395 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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("Person").Where(s=>s.Att ribute("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.
Oct 1 '08 #1
4 5069
On Wed, 01 Oct 2008 10:32:25 -0700, CSharper <cs******@gmx.comwrote:
[...]
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
Oct 1 '08 #2
On Oct 1, 1:08*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 01 Oct 2008 10:32:25 -0700, CSharper <cshar...@gmx.comwrote:
[...]
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.
Oct 1 '08 #3
On Wed, 01 Oct 2008 11:21:01 -0700, CSharper <cs******@gmx.comwrote:
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. :)
Oct 1 '08 #4
On Oct 1, 12:32 pm, CSharper <cshar...@gmx.comwrote:
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.Att ribute("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
Oct 1 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Scott Nonnenberg [MSFT] | last post by:
LINQ and C# 3.0 "Couldn't attend PDC but still want to talk to the C# team? This chat is your chance! Join the C# team to discuss the .NET Language Integrated Query Framework (LINQ) and newly...
14
by: Ralf Rottmann \(www.24100.net\) | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). ...
22
by: paululvinius | last post by:
Hi! Testing som Linq-expressions and tried to measure performance and compare it to pre-Linq programming. The folloing two methods are functional equal but the non-Linq one is twice as fast....
8
by: Andy | last post by:
Hi, I'm trying to add a where clause to my query: List<stringtypes = new List<string>(); types.Add( "A" ); types.Add( "B" ); query = query.Where( c =types.Contains( c.Type ) );
1
by: shapper | last post by:
Hi, I wonder, is there some tool that transforms SQL procedures to LINQ? :-) I want to use LINQ but I have so much work done in SQL that would be great to transform my SQL code to LINQ. ...
21
by: hrishy | last post by:
Hi Will LINQ be ported to Python ? regards Hrishy
3
by: Marc Gravell | last post by:
A lambda expression is a short form to write a delegate In /this/ case (LINQ-to-Objects): yes - but it could equally be compiled to an Expression, which is very, very different. A lambda...
2
by: Colin Han | last post by:
Hi, all, If I write follow code in c# method. The IDE will compile it to a complex construct method of System.Linq.Expression. Expression<Func<int>ex = () =10; will be compile to:...
5
by: Edwin | last post by:
I am trying to write an application among which one of the functions is to determine the number of unique extensions found in a directory and all of its sub directories. I am trying to use Linq to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.