[英]LINQ and result set mapping in class
here is the code这是代码
var customers = db.ExecuteQuery<Customer>(@"SELECT CustomerID, CompanyName, ContactName, ContactTitle,
Address, City, Region, PostalCode, Country, Phone, Fax
FROM dbo.Customers
WHERE City = {0}", "London");
foreach (Customer c in customers)
Console.WriteLine(c.ContactName);
code execute sql and retun a customer record.代码执行 sql 并返回客户记录。 my question how result can be stored in customer class automatically....which i do not understand.
我的问题是如何将结果自动存储在客户 class 中......我不明白。 if u see this line of code
db.ExecuteQuery<Customer>
from here we can understand that customer result will be return and customer data will be stored in customer class.如果你从这里看到这行代码
db.ExecuteQuery<Customer>
我们可以理解客户结果将被返回,客户数据将存储在客户 class 中。 how automatically data can be stored & assign to right property in customer class because a customer class CustomerID
property name could be CustID....then what will happen.如何自动存储数据并分配给客户 class 中的正确属性,因为客户 class
CustomerID
属性名称可能是 CustID....然后会发生什么。
the line db.ExecuteQuery<Customer>
is very confusing for me and i just do not understand a new customer instance will be created with return customer data....so plzz discuss in detail.行
db.ExecuteQuery<Customer>
对我来说非常令人困惑,我只是不明白将使用返回的客户数据创建一个新的客户实例......所以请详细讨论。
ExecuteQuery
just runs arbitrary SQL (after applying string.Format
-esque parameterization) and then materializes the IDataReader
results as a sequence of objects. ExecuteQuery
只是运行任意 SQL(在应用string.Format
-esque 参数化之后),然后将IDataReader
结果具体化为对象序列。
Note that the mappings between mapped types and the db-columns/type-members is well defined by the data-context ( usually via attributes on the members, but not always), but IIRC ExecuteQuery
does not apply these mapping (I'm happy to be corrected here).请注意,映射类型和 db-columns/type-members 之间的映射由数据上下文很好地定义(通常通过成员上的属性,但并非总是如此),但 IIRC
ExecuteQuery
不应用这些映射(我很高兴在这里更正)。 You can inspect this via db.Mapping.GetMetaType(typeof(Customer))
.您可以通过
db.Mapping.GetMetaType(typeof(Customer))
进行检查。
The fundamentals here are no different to if you executed这里的基本原理与您执行时没有什么不同
var cust = db.Customers.ToList();
except the db.Customers
version can apply more mappings etc.除了
db.Customers
版本可以应用更多映射等。
The materialization itself (ie creating the object and setting members) is an example of reflection and meta-programming;物化本身(即创建 object 和设置成员)是反射和元编程的一个例子; most ORMs and micro-ORMs will work pretty much the same there: inspect the type (
Customer
above) and inspect the fields in the reader - and then build some code on-the-fly (usually cached) that will create the new objects and set the members.大多数 ORM 和微 ORM 在那里的工作方式几乎相同:检查类型(上面的
Customer
)并检查阅读器中的字段 - 然后动态构建一些代码(通常是缓存的)来创建新对象和设置成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.