c# - Getting Results With Entity Framework -
i'm trying query db set, can't seem access data when results. here code i've tried.
query #1:
public list<customermain> testing() { var context = new customerentities(); var query = context.customermains .where(p=> p.firstname == "thor") .select(p => new customermain {firstname=p.firstname,lastname =p.lastname}).tolist(); return query; }
query #2:
public iqueryable<customermain> testing() { var context = new customerentities(); var query = context.customermains .where(p=> p.firstname == "thor") .select(p => new customermain {firstname=p.firstname,lastname =p.lastname}); return query; }
customermain
dbset
, if run either of , assign method var variable, gives me no options grab firstname
or lastname
variable. found can if convert customermain
, shouldn't return query in customermain
type already?
it gives me no options grab firstname or lastname variable
because method returns collection or queryable, not single entity. need iterate or select records:
var customers = testing(); foreach (customermain customer in customers) { // access customer.firstname }
or:
var customers = testing(); var onecustomer = customers.firstordefault(c => c.firstname == "thor"); // access onecustomer.firstname
or, if want method return 1 record (or null if none found):
public customermain findone(string firstname) { using (var context = new customerentities()) { var query = context.customermains.where(p => p.firstname == firstname); return query.firstordefault(); } }
Comments
Post a Comment