简体   繁体   English

区分EF查询和LINQ查询

[英]Distinguish EF query and LINQ query

I am new to Entity Framework. 我是Entity Framework的新手。 I am bit confused with the difference in EF query and LINQ query. 我对EF查询和LINQ查询的区别感到困惑。 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? 你能否告诉我所有这些查询是LINQ还是EF中的任何一个? I need to write EF queries for selecting entire row, few columns and also joins. 我需要编写EF查询来选择整行,几列和连接。 Your help or any relevant links would be highly appreciated. 您的帮助或任何相关链接将受到高度赞赏。

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

Sales_Details Table Sales_ID, Product_ID, Qunatity, Total_Amont Sales_Details表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). LINQ是一种在您选择的语言(VB,C#,.NET)中查询的方式。 It isn't directly related to EF. 它与EF没有直接关系。 EF is something that maps to your database and you use LINQ as a way to query the database. EF是映射到数据库的东西,您使用LINQ作为查询数据库的方法。 It's just syntax that you use paired with EF to get data. 它只是与EF配对使用的语法来获取数据。 You can also use LINQ on things such as collections. 您还可以在诸如集合之类的东西上使用LINQ。

There is no such thing as an EF query. 没有EF查询这样的东西。 The queries you use when working with EF are LINQ. 使用EF时使用的查询是LINQ。 LINQ however does a whole lot more. 然而,LINQ做了很多。

From EF documentation : 来自EF文档

Entity Framework is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. 实体框架是一种对象关系映射器(O / RM),它使.NET开发人员能够使用.NET对象处理数据库。 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#. 实体框架是C#中使用的库。

From this article about LINQ on MSDN: 这个文章关于LINQ 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)来表明查询是开发人员主要编程语言集成功能

LINQ is part of C#. LINQ是C#的一部分

General-purpose query facilities added to the .NET Framework apply to all sources of information, not just relational or XML data. 添加到.NET Framework的通用查询工具适用于所有信息源,而不仅仅是关系数据或XML数据。 This facility is called .NET Language-Integrated Query (LINQ). 此工具称为.NET语言集成查询(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. 所有EF都是抽象的数据库,并允许您使用它们,就好像它们是程序中的普通可用数据一样。

The standard query operators allow queries to be applied to any IEnumerable-based information source. 标准查询运算符允许将查询应用于任何基于IEnumerable的信息源。

You use LINQ queries and extension methods for enumerables that LINQ provides to work with the "enumerables" (DB abstractions) that EF provides you with. 您可以使用LINQ查询和扩展方法来获取LINQ提供的枚举,以使用EF为您提供的“枚举”(DB抽象)。

As far as I understand your question, all your code returns queries associated with linq. 据我所知,您的所有代码都会返回与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). 它们定义了要在数据库中完成的操作,但尚未访问数据库,因为它们没有定义执行命令(find,singleordefault,first,tolist等)。 Linq is used for building and executing queries, like an addition to the EF language that also can do that but has limited usage. Linq用于构建和执行查询,例如EF语言的添加,也可以这样做,但使用有限。 For databases linq builds queries and access the db via EF(or another ORM). 对于数据库,linq通过EF(或其他ORM)构建查询并访问数据库。

Some syntax like AsNoTracking(),Include(), ThenInclude() etc are EF syntax exclusively (meaning you have to refrence that library). 一些语法如AsNoTracking(),Include(),ThenInclude()等是EF语法专有(意味着你必须引用该库)。 Using linq syntax means you have to refrence linq(although most templates include it by default). 使用linq语法意味着您必须引用linq(尽管大多数模板默认包含它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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