合并2个LINQ列表后,我遇到了问题。我收到以下错误:查询包含对在不同数据上下文中定义的项的引用。
这就是我构建最终2个LINQ列表的方式:
CAPDBDataContext CAdb = new CAPDBDataContext();
USPDBDataContext USdb = new USPDBDataContext();
var ExceptionList = Special_texts.Where(x => x.name.Equals("ExceptionList"))
.Select(x => x.special_text1)
.AsEnumerable()
.Select(x => x.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries))
.SelectMany(x => x)
.ToList();
var USquery = from uscomp in USdb.UScompanies
join ussect in USdb.USsectors
on uscomp.sector_code equals ussect.sector_code
select new
{
uscomp.company_name,
uscomp.primary_exchange,
ussect.description
};
var CAtemp = from x in CAdb.companies
where !ShefalisList.Contains(x.ticker)
select x;
var CAquery = from temp in CAtemp
join casect in CAdb.sectors
on temp.sector_code equals casect.sector_code
select new
{
temp.company_name,
temp.primary_exchange,
casect.description
};
var mergedList = USquery.Union(CAquery).ToList();
我做错了什么?
答案 0 :(得分:5)
请改为:
var mergedList = USquery.AsEnumerable().Union(CAquery).ToList()
如果不将结果存入内存,则无法Union
查询不同的数据库。