I have a list of Tenants (call it TenantList), and it's composed of Tenant objects and they all have an ID property. How can I return an enumerable item composed of their ID properties?
+6
A:
You can use either
var result = TenantList.Select(t => t.ID)
or
var result = from tenant in TenantList select tenant.ID
Nader Shirazie
2010-07-21 16:05:19
What's the difference?
Malfist
2010-07-21 16:07:00
There's no difference. The query syntax is actually transformed into the extension method syntax under the covers. They're just different ways of expressing the same query. As you get more comfortable with linq, you will find that one or the other is more appropriate, depending on the context.
Nader Shirazie
2010-07-21 16:10:09
Syntactic sugar. The second form gets "preprocessed" into the first form when the code is compiled (shamelessly ripped off from Jon Skeet's C# in Depth).
Eric
2010-07-21 16:10:28