这就是我如何在两个具有不同列名的不同表之间执行联合, *我只从第二个表中选择日期和金额,并将 null 传递给其余列 *
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 中我不能创建像 mysql 这样的假列,因为我将 null 传递给我没有的列.. 它给出了错误,这是我的 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; }
}
订单支付类
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 引用了 Sales Class 中的 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) SQL联合规则比LINQ宽松。在 LINQ(通常)中,如果所有字段 name 和 type 都匹配,则两个匿名类型被认为是相同的。
(2) SQL to 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
(零)表示不可空类型。
更新:根据您的数据模型类,这里是实际的工作查询:
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);
关于c# - 如何像我在 Mysql 查询中执行的那样使用 Linq 执行联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36052346/