Home > Enterprise >  Entity Framework two types of queries
Entity Framework two types of queries

Time:04-16

What is the difference between these queries? What are their names and when should I use which?

var queryLondonCustomers = from cust in customers
                           where cust.City == "London"
                           select cust;


using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Where(b => b.Url.Contains("dotnet"))
        .ToList();
}

CodePudding user response:

Well, they're two entirely different queries for one thing.

But if you're just asking about the syntax difference, the former syntax is interpreted into the latter syntax during compilation. So any two queries which differ only in the syntax (and not in the actual query functionality) are for all intents and purposes identical.

For example, this:

from cust in customers
where cust.City == "London"
select cust

Is the same as this:

customers.Where(cust => cust.City == "London")

It's possible the compiler might produce something a little different, such as also appending a .Select() which doesn't actually do anything interesting. But the point is that the "LINQ syntax" of the former doesn't do anything special, it's just a syntactically different way of expressing the latter.

  • Related