简体   繁体   中英

Distinguish EF query and LINQ query

I am new to Entity Framework. I am bit confused with the difference in EF query and LINQ query. I have two tables and related queries listed below. Can you please tell me whether all these queries are LINQ or any of them are in EF? I need to write EF queries for selecting entire row, few columns and also joins. Your help or any relevant links would be highly appreciated.

Product_Details table Product_ID, Product_Name, Price, Item_Desc, Stock_Avaialble, Created_Date

Sales_Details Table Sales_ID, Product_ID, Qunatity, Total_Amont

var result = context.ProductDetails
                where ProductID == 10
                select new {ProductID, ProductName, Price}

var result = from prod in context.ProductDetails
                    where ProductID == 10
                    select new {ProductID, ProductName, Price}

var result = context.ProductDetails
                    .Where(p=>p.ProductID == 10)
                    .Select(p=> new Prod(p.ProductID, p.ProductName, p.Price))

var result1 = from prod in context.ProductDetails
                    join sales in context.SalesDetails on prod.ProductID == sales.ProductID
                    select new {prod.ProductID, prod.ProductName, sales.Qunatity, sales.TotalAmount}

Thanks Peter

LINQ is a way of querying within you language of choice (VB, C#, .NET). It isn't directly related to EF. EF is something that maps to your database and you use LINQ as a way to query the database. It's just syntax that you use paired with EF to get data. You can also use LINQ on things such as collections.

There is no such thing as an EF query. The queries you use when working with EF are LINQ. LINQ however does a whole lot more.

From EF documentation :

Entity Framework is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write.

Entity Framework is a library used in C#.

From this article about LINQ on MSDN:

We use the term language-integrated query (LINQ) to indicate that query is an integrated feature of the developer's primary programming languages .

LINQ is part of C#.

General-purpose query facilities added to the .NET Framework apply to all sources of information, not just relational or XML data. This facility is called .NET Language-Integrated Query (LINQ).

The key here is the phrase " apply to all sources of information ". All EF does is abstract away databases, and allow you to work with them as if they were normal enumerables in your program.

The standard query operators allow queries to be applied to any IEnumerable-based information source.

You use LINQ queries and extension methods for enumerables that LINQ provides to work with the "enumerables" (DB abstractions) that EF provides you with.

As far as I understand your question, all your code returns queries associated with linq. They define operations to be done in the database but have not taken the trip to the db yet because they don't define execute commands(find, singleordefault, first, tolist etc). Linq is used for building and executing queries, like an addition to the EF language that also can do that but has limited usage. For databases linq builds queries and access the db via EF(or another ORM).

Some syntax like AsNoTracking(),Include(), ThenInclude() etc are EF syntax exclusively (meaning you have to refrence that library). Using linq syntax means you have to refrence linq(although most templates include it by default).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM