项目代码(伪代码)
1、更改前:r1.Concat(r2) //这里会报错
public IQueryable<General> GetU8SchedulingOrderPlan(DateTime? start, DateTime? end = null)
{
var res1 = u8Plan.GetU8SchedulingOrderPlan()
.Where(e => e.IQtyFjMofy > 0 && (e.IStatus == 1 || e.IStatus == 2) && e.DLastXqdate != null)
.Select(e => new General
{
Date = e.DLastXqdate,
ItemCode = e.CInvCode,
CustomerCode = e.CCusCode,
Qty = e.IQtyFjMofy ,
Type="U8已审批订单"
});
return res1;
}
public IQueryable<General> GetU8ShippingPlan(DateTime? start, DateTime? end=null)
{
var res1 = u8Plan.GetU8ShippingPlan()
.Select(e => new General
{
Date = e.Dplandldate,
ItemCode = e.Cinvcode,
CustomerCode = e.Ccuscode,
Qty = e.Iquantity,
Type = "发货计划"
});
return res1;
}
var r1 = GetU8SchedulingOrderPlan
var r2 = GetU8ShippingPlan()
r1.Concat(r2)
2、更新后
public IQueryable<General> GetU8SchedulingOrderPlan(DateTime? start, DateTime? end = null)
{
var res1 = u8Plan.GetU8SchedulingOrderPlan()
.Where(e => e.IQtyFjMofy > 0 && (e.IStatus == 1 || e.IStatus == 2) && e.DLastXqdate != null)
.Select(e => new General
{
Date = e.DLastXqdate,
ItemCode = Convert.ToString(e.CInvCode),
CustomerCode = Convert.ToString(e.CCusCode),
Qty = (int)e.IQtyFjMofy ,
Type="U8已审批订单"
});
return res1;
}
public IQueryable<General> GetU8ShippingPlan(DateTime? start, DateTime? end=null)
{
var res1 = u8Plan.GetU8ShippingPlan()
.Select(e => new General
{
Date = e.Dplandldate,
ItemCode = Convert.ToString(e.Cinvcode),
CustomerCode = Convert.ToString(e.Ccuscode),
Qty = (int)e.Iquantity,
Type = "发货计划"
});
return res1;
}
var r1 = GetU8SchedulingOrderPlan
var r2 = GetU8ShippingPlan()
r1.Concat(r2) //这里会报错
分析:主要原因是string字段和demical字段导致
1、在SQL SERVER中
- stirng类型有长度的区别:nvarchar(50)、nvarchar(60)、nvarchar(max)
- demical类型有精度的区分:decimal(30, 10)、decimal(30, 15)
2、虽然在C#的项目中他们都是decimal、string类型的属性,但是在sql server还有精度的区分
3、Linq中的Concat转成Sql Server的sql是union all语句
4、在SQL SERVER中,可能是union all认为同钟数据类型即使存在精度差异,也可以判定应该合并的两个string类型或demical字段存在不同,而报错无法合并。
解决方案
1、string类型在linq中加上:Convert.ToString(),转成sql就是Convert(nvarchar(max),字段)
2、对于decimal,如果使用Convert.ToDecimal(),也是不行的,这个没办法转成SQL,所以一旦合并还是会报错。目前解决方案是强转成int类型