i have following working entity frameowrk code creating dto entity framework result. created invoicedto object has list<servicechargedto>
property inside it. done referring mapping linq query results dto class
public class invoicedto { public int invoiceid { get; set; } public list<servicechargedto> servicechargelineitems { get; set; } public decimal invoicetotal { get; set; } } public invoicedto getinvoicebyid(int invoiceidparam) { invoicedto invoice=null; using (var db = new portalentities()) { var invoices = in db.invoices a.invoiceid == invoiceidparam select new invoicedto { invoiceid = a.invoiceid, invoicetotal = a.invoiceamount, servicechargelineitems = (from b in db.invoiceservicexrefs a.invoiceid == b.invoiceid select new servicechargedto { servicechargeid = b.servicechargeid, quantity = b.servicecharge.qty, unitprice=b.servicecharge.unitprice, amount=b.servicecharge.amount } ).tolist() }; invoice = invoices.firstordefault(); } return invoice; }
here data related tables retrieved.
invoiceservicexrefs.servicecharge.qty
i need change approach , make convert method.
public invoicedto getinvoicebyid(int invoiceidparam) { var invoice2 = null; using (var db = new portalentities()) { var invoices2 = b in db.invoices b.invoiceid == invoiceidparam select b; invoice2 = toinvoicedto(invoices2.firstordefault()); } return invoice2; }
but navigation properties not getting listed when type invoice.invoiceservicexrefs.
.
how navigation properties correctly , create invoicedto
object list<servicechargedto>
?
you can use pretty same code map servicechargelineitems, used in initial query
private static invoicedto toinvoicedto(invoice invoice) { invoicedto e = new invoicedto() { invoiceid = invoice.invoiceid, invoicetotal = invoice.totalamount }; e.servicechargelineitems = invoice.invoiceservicexrefs. select(b => new servicechargedto { servicechargeid = b.servicechargeid, quantity = b.servicecharge.qty, unitprice = b.servicecharge.unitprice, amount = b.servicecharge.amount }).tolist() return e; }
update:
following how called. note use of tolist() on invoicesquery. approach issues multiple queries.
var invoicesquery = in db.invoices a.invoiceid == invoiceidparam select a; list<invoicedto> listtest = invoicesquery.tolist().select(x => toinvoicedto(x)).tolist();
Comments
Post a Comment