c# - How to populate my base Customer with EF -
see class structure first.
public class customerbase { public int customerid { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string address1 { get; set; } public string address2 { get; set; } public string phone { get; set; } public string fax { get; set; } } public class customer : customerbase { public virtual list<addresses> addresses { get; set; } } public class addresses { [key] public int addressid { get; set; } public string address1 { get; set; } public string address2 { get; set; } public bool isdefault { get; set; } public virtual list<contacts> contacts { get; set; } public int customerid { get; set; } public virtual customer customer { get; set; } } public class contacts { [key] public int contactid { get; set; } public string phone { get; set; } public string fax { get; set; } public bool isdefault { get; set; } public int addressid { get; set; } public virtual addresses customer { get; set; } } public class testdbcontext : dbcontext { public testdbcontext() : base("name=testdbcontext") { } public dbset<customer> customer { get; set; } public dbset<addresses> addresses { get; set; } public dbset<contacts> contacts { get; set; } }
now way trying populate customer base getting error.
var bscustomer1 = (from c in db.customer (c.customerid == 2) select new { customerid = c.customerid, firstname = c.firstname, lastname = c.lastname, addresses = (from ad in c.addresses (ad.isdefault == true) cts in ad.contacts (cts != null && cts.isdefault == true) select ad).tolist(), }).tolist() .select(x => new customerbase { customerid = x.customerid, firstname = x.firstname, lastname = x.lastname, address1 = x.addresses.select(a => a.address1).singleordefault(), address2 = x.addresses.select(a => a.address2).singleordefault(), phone = x.addresses.select(c => c.contacts.select(cd => cd.phone).singleordefault()), fax = x.addresses.select(c => c.contacts.select(cd => cd.fax).singleordefault()) }).tolist();
as per situation single customer may have multiple address there should 1 default 1 pulling. single address may have multiple contacts details there should 1 default 1 pulling.
address1,address2, phone , fax in base customer class. want pull single data address , contacts tables based on isdefault true , populate customer. not in linq. not being able compose query. please me compose it. thanks
try code below, guess may fit request.
var bscustomer1 = db.customer.where(p => p.customerid == 2) .select(x => new customerbase { customerid = x.customerid, firstname = x.firstname, lastname = x.lastname, address1 = x.addresses.first(a => a.isdefault).address1, address2 = x.addresses.first(a => a.isdefault).address2, phone = x.addresses.first(a => a.isdefault).contacts.first(c => c.isdefault).phone), fax = x.addresses.first(a => a.isdefault).contacts.first(c => c.isdefault).fax) }).tolist();
Comments
Post a Comment