我正在尝试使用union如下:
var query1 = from c in dc.Hotel_Meals_TBLs
where c.CHDLunch != "0" && c.CHDLunch != ""
select new
{
SERVICE_CODE = c.HotelCodeID,
SERVICE_NAME = c.HotelName,
ROOM_CATEGORY = "",
ROOM_TYPE = "",
VARIANCE_NAME = "CHILDLUNCH",
MARKET = "WW",
CONTRACT_BUSINESS_YEAR = "2014/2015",
CONTRACT_START_DATE = "01/11/2014",
CONTRACT_END_DATE = "30/10/2015",
TYPE = "CHILD POLICY",
CURRENCY = c.CurrencyCode,
PERIOD_NAME = "",
PERIOD_START_DATE = "",
PERIOD_END_DATE = "",
PRICE = c.Lunch,
PERCENTAGE = c.CHDLunch + "%",
NUM_TO_STAY = "",
NUM_TO_PAY = "",
PENALTY_TIME_LIMIT = "",
PENALTY_NO_DAY_BEFORE = "",
CHILD_REFERENCE = "",
TEXT = c.ChildPolicy
};
var query2 = from c in dc.Hotel_Meals_TBLs
from d in dc.HotelPeriod_TBLs
from f in dc.HotelRoom_TBLs
where c.HotelCodeID == d.HotelCodeID && c.HotelCodeID == f.HotelCodeID && f.DBL_HighSeason != 0 && d.PeriodName == "High"
select new
{
SERVICE_CODE = c.HotelCodeID,
SERVICE_NAME = c.HotelName,
ROOM_CATEGORY = f.RoomName,
ROOM_TYPE = "DBL",
VARIANCE_NAME = "",
MARKET = "WW",
CONTRACT_BUSINESS_YEAR = "2014/2015",
CONTRACT_START_DATE = "01/11/2014",
CONTRACT_END_DATE = "30/10/2015",
TYPE = "COST",
CURRENCY = c.CurrencyCode,
PERIOD_NAME = d.PeriodName,
PERIOD_START_DATE = d._From,
PERIOD_END_DATE = d._To,
PRICE = f.SGL_LowSeason,
PERCENTAGE = "",
NUM_TO_STAY = "",
NUM_TO_PAY = "",
PENALTY_TIME_LIMIT = "",
PENALTY_NO_DAY_BEFORE = "",
CHILD_REFERENCE = "",
TEXT = ""
};
var result = query1.Union(query2);
我得到了这个错误:
'System.Linq.IQueryable<AnonymousType#1>'
不包含'Union'的定义,并且最佳扩展方法重载'System.Linq.ParallelEnumerable.Union<TSource>(System.Linq.ParallelQuery<TSource>, System.Collections.Generic.IEnumerable<TSource>)'
有一些无效的参数实例参数:无法从
'System.Linq.IQueryable<AnonymousType#1>'
转换为'System.Linq.ParallelQuery<AnonymousType#2>'
真的我多次使用这种方法而没有出错...你知道这个查询有什么问题吗?
答案 0 :(得分:2)
错误消息表明.NET认为这两种匿名类型不同。为了使两个匿名类型相同,它们必须具有相同数量的具有相同名称和类型的属性。我已经浏览了你的属性列表,它们看起来在同一个顺序中具有相同的名称。但是,您可能在这些方面有不同的类型:
输入1
PERIOD_START_DATE = ""
PERIOD_END_DATE = ""
PRICE = c.Lunch,
TEXT = c.ChildPolicy
类型2
PERIOD_START_DATE = d._From
PERIOD_END_DATE = d._To
PRICE = f.SGL_LowSeason
TEXT = ""
确保d._From
,d._To
和c.ChildPolicy
都是字符串,且c.Lunch
和f.SGL_LowSeason
属于同一类型(例如,可能是一个int
,另一个是decimal
)。
答案 1 :(得分:1)
您必须创建一个包含所需属性的ViewModel,并将所有值分配给viewmodel属性,并且在查询中select语句将如下:
select new ViewModel{
ViewmodelProperties = Value,
};
像:
var query1 = from c in dc.Hotel_Meals_TBLs
where c.CHDLunch != "0" && c.CHDLunch != ""
select new ViewModel
{
SERVICE_CODE = c.HotelCodeID,
SERVICE_NAME = c.HotelName,
ROOM_CATEGORY = "",
ROOM_TYPE = "",
VARIANCE_NAME = "CHILDLUNCH",
MARKET = "WW",
CONTRACT_BUSINESS_YEAR = "2014/2015",
CONTRACT_START_DATE = "01/11/2014",
CONTRACT_END_DATE = "30/10/2015",
TYPE = "CHILD POLICY",
CURRENCY = c.CurrencyCode,
PERIOD_NAME = "",
PERIOD_START_DATE = "",
PERIOD_END_DATE = "",
PRICE = c.Lunch,
PERCENTAGE = c.CHDLunch + "%",
NUM_TO_STAY = "",
NUM_TO_PAY = "",
PENALTY_TIME_LIMIT = "",
PENALTY_NO_DAY_BEFORE = "",
CHILD_REFERENCE = "",
TEXT = c.ChildPolicy
};
var query2 = from c in dc.Hotel_Meals_TBLs
from d in dc.HotelPeriod_TBLs
from f in dc.HotelRoom_TBLs
where c.HotelCodeID == d.HotelCodeID && c.HotelCodeID == f.HotelCodeID && f.DBL_HighSeason != 0 && d.PeriodName == "High"
select new ViewModel
{
SERVICE_CODE = c.HotelCodeID,
SERVICE_NAME = c.HotelName,
ROOM_CATEGORY = f.RoomName,
ROOM_TYPE = "DBL",
VARIANCE_NAME = "",
MARKET = "WW",
CONTRACT_BUSINESS_YEAR = "2014/2015",
CONTRACT_START_DATE = "01/11/2014",
CONTRACT_END_DATE = "30/10/2015",
TYPE = "COST",
CURRENCY = c.CurrencyCode,
PERIOD_NAME = d.PeriodName,
PERIOD_START_DATE = d._From,
PERIOD_END_DATE = d._To,
PRICE = f.SGL_LowSeason,
PERCENTAGE = "",
NUM_TO_STAY = "",
NUM_TO_PAY = "",
PENALTY_TIME_LIMIT = "",
PENALTY_NO_DAY_BEFORE = "",
CHILD_REFERENCE = "",
TEXT = ""
};
var result = query1.Union(query2);