如何在C#中合并两个不同实体的结果?

时间:2016-01-19 07:46:20

标签: c# linq-to-entities

IQueryable<v_Search> query = from pd in context.v_Search select pd;
IQueryable<t_HistoricAccountImport> xschistory  = from pd in context.t_HistoricAccountImport select pd;

query = query.Where(x => names.Any(y => x.insured_name.Contains(y)));
xschistory = xschistory.Where(xsc => names.Any(y => xsc.insured_name.Contains(y)));

两个实体都存在于同一上下文中

现在,当我尝试下面的代码来组合两个结果时,使用union或concat,这不会发生。

尝试1)

var results = query.concat(xschistory); //Caused error
var result1 = query.union(xschistory); //this also throws error

我还尝试从具有相似数据类型和值的实体中选择列并尝试合并,

尝试2)

    var res1 = query.ToList().Select
                        (x => new 
                        { x.insured_name ,
                          x.ReferenceNum
                        });
    var res2 = xschistory.ToList().Select
                        (y=> new 
                        {
                            y.insured_name,
                            y.program_id
                        });

 var finalResults = res1.Union(res2); //Still got Error

无论如何要实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

您可以做的一件事就是将它们绑定到

 List<v_search> a; List<t_HistoricAccountImport> b;

然后

var together = a.Cast<object>().Concat(b.Cast<object>()).ToList();//cast preferably to something they both inherit from, and is not 'object'.

你现在不能a.concat(b)因为对象的类型不同。