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; }
}
我想如你所說 – Akash
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
仍然得到相同的錯誤 錯誤\t \t 4實例論證:不能從轉換 'System.Collections.Generic.IEnumerable' 到 'System.Linq.IQueryable ' –
Akash