c# - Linq Query with join on subquery -


basically i'm trying write query joins on select top 1 second table like:

select sum(pinfo.quantity + p.itemcount),     i.owner    invoice     join purchase_info pinfo       on pinfo.invoice = i.invid     join (select distinct sku,                           productlineid,                           itemcount             products productlineid in (13, 14)) p       on p.sku = pinfo.item  i.owner = 22623  group i.owner 

here's pathetic attempt in linq has invalid syntax, ideas appreciated.

(from in _invoicerepository.table  join pi in _purchaseinforepository.table on i.invoiceid equals pi.invoiceid  join p in (from p2 in _productrepository.table p2.sku == pi.item select new { p2.sku, p2.itemcount }).take(1)  on pi.item equals p.sku   i.memberid == memberid &&  (p.productlineid == (int)productlines.inkcartridges ||   p.productlineid == (int)productlines.tonercartridges)  select pi.quantity * p.itemcount) .defaultifempty(0) .sum(); 

here first stab @ this.

from sql, looks want find how many ink , toner cartridges particular customer has ordered ever.

this should give same results sql (this depending on order of products table since taking top 1 without sort of ordering being done:

  var count = in _invoicerepository.table               i.ownerid == memberid               select new                 {                   ownerid = i.ownerid,                   totalproductcount = i.purchases.sum(pro => pro.products                                                       .where(p => p.productlineid == (int)productlines.inkcartridges ||                                                          p.productlineid == (int)productlines.tonercartridges)                                                       .take(1)                                                       .sum(p => p.itemcount * pro.quantity))                 }; 

since did not know the classes of 3 objects (invoice, purchaseinfo, , product) made guess @ are:

invoice class: assume has list/collection of purchaseinfos

public class invoice {   public int id { get; set; }   public int ownerid { get; set; }    public list<purchaseinfo> purchases { get; set; } } 

purchaseinfos: invoice has multiple purchaseinfos, each 1 links (ideally) 1 product since sku not unique assome has list/collection of products in it.

public class purchaseinfo {   public int id { get; set; }   public int quantity { get; set; }    public int invoiceid { get; set; }   public invoice invoice { get; set; }    public int item {get;set;}   public list<product> products { get; set; } } 

product class: assome there id field (not shown) or composite primary key somewhere

public class product {   public int sku { get; set; }   public int productlineid { get; set; }   public int itemcount { get; set; }    public list<purchaseinfo> purchaseinfos { get; set; } } 

hopefully can take need. if way off, please update question class definitions (you can remove unneeded properities if want) better answer can produced.


Comments

Popular posts from this blog

How can I fetch data from a web server in an android application? -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -

jquery - How can I dynamically add a browser tab? -