c# - LINQ and result set mapping in class -


here code

        var customers = db.executequery<customer>(@"select customerid, companyname, contactname, contacttitle,     address, city, region, postalcode, country, phone, fax      dbo.customers     city = {0}", "london");  foreach (customer c in customers)    console.writeline(c.contactname); 

code execute sql , retun customer record. question how result can stored in customer class automatically....which not understand. if u see line of code db.executequery<customer> here can understand customer result return , customer data stored in customer class. how automatically data can stored & assign right property in customer class because customer class customerid property name custid....then happen.

the line db.executequery<customer> confusing me , not understand new customer instance created return customer data....so plzz discuss in detail.

executequery runs arbitrary sql (after applying string.format-esque parameterization) , materializes idatareader results sequence of objects.

note mappings between mapped types , db-columns/type-members defined data-context (usually via attributes on members, not always), iirc executequery does not apply these mapping (i'm happy corrected here). can inspect via db.mapping.getmetatype(typeof(customer)).

the fundamentals here no different if executed

var cust = db.customers.tolist(); 

except db.customers version can apply more mappings etc.

the materialization itself (i.e. creating object , setting members) example of reflection , meta-programming; orms , micro-orms work pretty same there: inspect type (customer above) , inspect fields in reader - , build code on-the-fly (usually cached) create new objects , set members.


Comments