C# 如何像我在Mysql查询中执行的那样使用Linq执行联合

C# 如何像我在Mysql查询中执行的那样使用Linq执行联合,c#,mysql,linq,linq-to-entities,union,C#,Mysql,Linq,Linq To Entities,Union,这就是我如何在两个不同的表之间使用不同的列名执行并集的方法, *我只从第二个表中选择日期和金额,并将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.

这就是我如何在两个不同的表之间使用不同的列名执行并集的方法, *我只从第二个表中选择日期和金额,并将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 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; }

}
公共部分类销售
{
公共int Id{get;set;}
公共int PartyId{get;set;}
公共int ItemId{get;set;}
公共可为空的请求数量{get;set;}
公共可空可用数量{get;set;}
公共字符串Desc{get;set;}
公共可空金额{get;set;}
公共字符串备注{get;set;}
公共可空日期{get;set;}
公共整数总数{get;set;}
公共虚拟项项{get;set;}
公共虚拟partydeail partydeail{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; }
}
公共部分类订单支付
{
公共int id{get;set;}
公共可空partyId{get;set;}
公共可空金额{get;set;}
公共可空日期{get;set;}
公共字符串备注{get;set;}
公共虚拟partydeail partydeail{get;set;}
}
引用Sales类中的itemId的ItemClass

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 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; }

}
公共部分类项
{
公共项目()
{
this.sales=new HashSet();
this.ordereds=new HashSet();
this.stocks=newhashset();
this.productions=new HashSet();
this.productions1=新的HashSet();
this.finishs=new HashSet();
this.productions11=新的HashSet();
}
公共int Id{get;set;}
公共字符串ItemName{get;set;}
公共字符串类别{get;set;}
公共字符串状态{get;set;}
公共可空minqty{get;set;}
公共虚拟ICollection销售{get;set;}
}

需要提及的一些事情:

(1) SQL联合规则比LINQ更宽松。在LINQ中(通常情况下),如果所有字段nametype匹配,则两个匿名类型被视为相同

(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
(零)

更新:根据您的数据模型类,以下是实际的工作查询:

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);

var query1=(从lam.sales.AsEnumerable()中的s开始,其中s.PartyId==1选择新的{Date=s.Date,item=s.ItemId,Qty=s.ReqQty,Amount=s.Amount,TotalAmount=s.TotalAmount});var query2=(从lam.paydetails.AsEnumerable()中的q开始,其中q.partyId==1选择新的{Date=q.Date,item=0,Qty=0,Amount=0,TotalAmount=q.Amount});var queryresult=query1.Concat(query2);仍然收到相同的错误错误错误4实例参数:无法从“System.Collections.Generic.IEnumerable”转换为“System.Linq.IQueryable”,此处paydetail表没有Qty=0、Amount=0、TotalAmount=0列,并且sales表有Reqty、Amount、,totalamount可为null的int和ItemId不可为null的int只需检查类型并在需要时使用强制类型转换即可使它们相等。比如
item=0,requqty=(int?.null,Amount=(int?.null
)。顺便说一句,您不需要
AsEnumerable()
,最好将其删除,以便对database.var query1=(从lam.sales中的s.PartyId==1选择新的{Date=s.Date,Itemid=s.Itemid,Qty=s.reqty,Amount=s.Amount,TotalAmount=s.TotalAmount});var query2=(从lam.paydetails中的q开始,其中q.partyId==1选择新的{Date=(DateTime?)q.Date,Itemid=0,Qty=(int?)null,Amount=(int?)null,TotalAmount=q.Amount});var queryresult=query1.Concat(query2);