2016-03-17 41 views
1

這是我如何執行聯合使用不同列名的兩個不同表, *我只從第二個表中選擇日期和金額,並將null傳遞給其餘的列*如何使用Linq執行聯合,因爲我在Mysql查詢中執行

string query="select `Date`,`ItemName`,`ReqQty`, `Amount`,`TotalAmount` from ((SELECT s.`Date` ,i.`ItemName`, s.`ReqQty`,s.`Amount`,s.`TotalAmount` FROM `sale` as s join items as i on s.itemid=i.id where s.`partyId`='" + ddllist.SelectedValue.ToString() + "') Union all ((select p.date,'','','Recieved', p.`amount` as Totalamount from `OrderPayment` as p where p.`partyId`='" + ddllist.SelectedValue.ToString() + "'))) tb order by `Date`"; 

但在LINQ,我傳遞null來,我沒有列我不能創造像MySQL虛假列..提示錯誤,這裏是我的LINQ嘗試

 var query1 = (from s in lam.sales where s.PartyId == 1 select new { Date = s.Date, Itemid = s.ItemId, Qty = s.ReqQty, Amount = s.Amount, TotalAmount = s.TotalAmount }); 

     var query2 = (from q in lam.paydetails where q.partyId == 1 select new { Date =(DateTime?) q.Date, Itemid = 0, Qty = (int?)null, Amount = (int?)null, TotalAmount = q.amount }); 
     var queryresult = query1.Concat(query2); 

銷售類

public partial class sale 
{ 
    public int Id { get; set; } 
    public int PartyId { get; set; } 
    public int ItemId { get; set; } 
    public Nullable<int> ReqQty { get; set; } 
    public Nullable<int> AvaibleQty { get; set; } 
    public string Desc { get; set; } 
    public Nullable<int> Amount { get; set; } 
    public string Remarks { get; set; } 
    public Nullable<System.DateTime> Date { get; set; } 
    public int TotalAmount { get; set; } 

    public virtual item item { get; set; } 
    public virtual partydetail partydetail { get; set; } 
} 

OrderPayment類

public partial class orderpayment 
{ 
    public int id { get; set; } 
    public Nullable<int> partyId { get; set; } 
    public Nullable<decimal> amount { get; set; } 
    public Nullable<System.DateTime> Date { get; set; } 
    public string Remarks { get; set; } 

    public virtual partydetail partydetail { get; set; } 
} 

itemClass時這是refencing到的itemId在銷售類

public partial class item 
{ 
    public item() 
    { 
     this.sales = new HashSet<sale>(); 
     this.ordereds = new HashSet<ordered>(); 
     this.stocks = new HashSet<stock>(); 
     this.productions = new HashSet<production>(); 
     this.productions1 = new HashSet<production>(); 
     this.finishes = new HashSet<finish>(); 
     this.productions11 = new HashSet<production>(); 
    } 

    public int Id { get; set; } 
    public string ItemName { get; set; } 
    public string Category { get; set; } 
    public string Status { get; set; } 
    public Nullable<int> minqty { get; set; } 

    public virtual ICollection<sale> sales { get; set; } 

} 

回答

1

有些事情要提:

(1)SQL工會規則比更輕鬆LINQ。在LINQ(和一般),兩個匿名類型被認爲是相同的,如果所有字段比賽。

(2)SQL中的LINQ工會操作映射如下:

SQL   LINQ 
========= ======= 
UNION  Union 
UNION ALL Concat 

它們應用到你的情況:

var query1 = (from s in lam.sales.AsEnumerable() where s.PartyId == 1 
       select new { s.Date, s.item, s.ReqQty, s.Amount, s.TotalAmount }); 

var query2 = (from q in lam.paydetails where q.partyId == 1 
       select new { q.Date, item = "", ReqQty = "", Amount = "", TotalAmount = q.amount }); 

var queryresult = query1.Concat(query2); 

注意上面確保字段名稱匹配,但你還需要確保類型匹配(因爲我沒有他們,我不能這樣做)。舉例來說,如果s.ReqQty和/或s.Amount都是數字類型,你有null爲可空類型或0(零)更換相應""非可空類型。

UPDATE:根據您的數據模型類,這裏是實際工作查詢:

var query1 = (from s in db.sales where s.PartyId == 1 
       select new { s.Date, s.item, s.ReqQty, s.Amount, TotalAmount = (decimal?)s.TotalAmount }); 

var query2 = (from q in db.paydetails where q.partyId == 1 
       select new { q.Date, item = (item)null, ReqQty = (int?)null, Amount = (int?)null, TotalAmount = q.amount }); 

var queryresult = query1.Concat(query2); 
+0

我想如你所說 – Akash

+0

VAR QUERY1 =(從s在lam.sales.AsEnumerable(),其中s .PartyId == 1選擇新的{日期= s.Date,項= s.ItemId,數量= s.ReqQty,金額= s.Amount,總金額= s.TotalAmount}); VAR QUERY2 =(從lam.paydetails.AsEnumerable)q個(q.partyId == 1選擇新的{日期= q.Date,項= 0,數量= 0,量= 0,總金額= q.amount} ); VAR QueryResult中= query1.Concat(QUERY2); – Akash

+0

仍然得到相同的錯誤 錯誤\t \t 4實例論證:不能從轉換 'System.Collections.Generic.IEnumerable ' 到 'System.Linq.IQueryable ' – Akash