Which one is better than lambda expression or
I have a LINQ to SQL expression
from msg in TblUserMessages
join user in Aspnet_Users on msg.FromUserID equals user.UserId
select new {
msg.FromUserID,
msg.ToUserID,
msg.MessageLocationID,
msg.MessageID,
user.UserName
}
And after the LINQ method expression:
TblUserMessages
.Join (
Aspnet_Users,
msg => msg.FromUserID,
user => user.UserId,
(msg, user) =>
new
{
FromUserID = msg.FromUserID,
ToUserID = msg.ToUserID,
MessageLocationID = msg.MessageLocationID,
MessageID = msg.MessageID,
UserName = user.UserName
}
)
Both of them return the same result set. This, for example,
82522f05-2650-466a-a430-72e6c9fb68b7 6b2a174a-8141-43d2-b3ad-5b199bcbfcae 1 1 waheed
Which one is better to use. FIRST
one or SECOND
one.
thank
source to share
They are equivalent. They don't just return the same result set - they compile to the same code.
Use query expressions or dot notation for specific cases depending on readability. For unions, I find the dot notation to be quite cumbersome, but I use it for situations where I only have one or two clauses (usually where / select is). Even with two sentences (usually where to choose), I find dot notation nice if you need to use it anyway. For example, I like:
var query = people.Where(person => person.Age > 18)
.Select(person => person.Name)
.Skip(100)
.Take(10);
over
var query = (from person in people
where person.Age > 18
select person.Name)
.Skip(100)
.Take(10);
For more complex queries (like joins), I would just split the two:
var baseQuery = from person in people
where person.Age > 18
join company on person.CompanyId equals company.CompanyId
select new { person.Name, company.Name };
var fullQuery = baseQuery.Skip(100)
.Take(10);
I just find this separation makes it easier to read.
I find it very helpful for developers to understand at least the basics of what query expressions do - the fact that they mostly translate to dot notation and that the language itself doesn't know anything about LINQ to Objects, LINQ to SQL, etc. .; this is just a case of following the appropriate template. This is a great design, which means that query expressions only affect one bit of the language specification.
source to share
There is no better one. Use what you like. In this case, I would go with the query syntax, as I find it more readable than the second one. Moreover, since we are writing SQL anyway, I think this syntax is more like it. But I'm sure others will disagree and tend to choose the lamda version.
I usually use the latter btw syntax as I prefer the lambda syntax because it is often more readable and shorter.
See also this SO question .
source to share
There is one slight difference between the two types of syntax. The one query expression will take longer to compile because the compiler has to convert it to dot syntax. Of course, this is almost completely irrelevant, because:
- minimal compilation,
- The emitted IL to work with at runtime is the same in both cases.
source to share
I use Lambda Expressions when I have to build a query dynamically and LINQ to SQL when the query is fully known.
IQueryable<IncomeDetailsEntity> query;
if (!string.IsNullOrEmpty(regioncode))
{
if (!string.IsNullOrEmpty(compcode))
{
query = db.IncomeDetailsEntities.Where(i => i.RegionCode == regioncode && i.CompanyCode == compcode);
}
else
{
query = db.IncomeDetailsEntities.Where(i => i.RegionCode == regioncode);
}
}
else
{
query = db.IncomeDetailsEntities;
}
return query.Select(i => new { i.RegionCode, i.Budget });
source to share