[英]Linq AddRange() for objects derived from interface
I'm trying to get results using LINQ in WPF on Visual Studio 2015 using Entity Framework 6 (EF). 我正在尝试使用Entity Framework 6(EF)在Visual Studio 2015上使用WPF中的LINQ获得结果。 The results need to appear in one ListView
for the employee's tests. 结果需要出现在一个ListView
用于员工的测试。 I have two EF entities: DrugTests
and AlcoholTests
. 我有两个EF实体: DrugTests
和AlcoholTests
。 In my project, I have some view models to map these to: DrugTestViewModel
and AlcoholTestViewModel
, both derived from the interface ITestViewModel
. 在我的项目中,我有一些视图模型将这些模型映射到: DrugTestViewModel
和AlcoholTestViewModel
,两者都来自ITestViewModel
接口。
The two VMs share many properties while also having their own unique ones (hence the need for two entities). 这两个VM共享许多属性,同时也有自己独特的属性(因此需要两个实体)。 I'm using LINQ to return a Task<ObservableCollection<ITestViewModel>>
, first fetching from the DrugTests
entity: 我正在使用LINQ返回Task<ObservableCollection<ITestViewModel>>
,首先从DrugTests
实体获取:
var query = (from dt in context.DrugTests
.Where(x => x.employeeID.Equals(employeeId))
select new DrugTestViewModel
{
EmployeeId = dt.employeeID,
TestType = "Drug",
OrderedDate = dt.orderedDate,
// set other properties
}
).ToList();
Then I try to add to the query using this: 然后我尝试使用这个添加到查询:
query.AddRange(from at in context.AlcoholTests
.Where(x => x.employeeID.Equals(employeeId))
select new AlcoholTestViewModel
{
EmployeeId = at.employeeID,
TestType = "Alcohol",
// set other properties...
}
);
It gives the following exception on the AddRange()
: 它在AddRange()
上给出以下异常:
Argument 1: cannot convert from
'System.Linq.IQueryable<MySolution.ViewModel.AlcoholTestViewModel>'
to'System.Collections.Generic.IEnumerable<MySolution.ViewModel.DrugTestViewModel>'
参数1:无法从'System.Linq.IQueryable<MySolution.ViewModel.AlcoholTestViewModel>'
转换为'System.Collections.Generic.IEnumerable<MySolution.ViewModel.DrugTestViewModel>'
What am I doing wrong? 我究竟做错了什么?
A little late but I would like to share this another solution. 有点晚了,但我想分享另一个解决方案。 I would use Concat this way: 我会用这种方式使用Concat :
var query=...
var query2=..
var result= query.AsEnumerable<ITestViewModel>().Concat(query2);
This way you maintain deferred execution in case you don't need to materialize the result yet. 这样,您可以在不需要实现结果的情况下维护延迟执行。
The problem is that the query
variable type is List<DrugTestViewModel>
while you need it to be of type List<ITestViewModel>
. 问题是query
变量类型是List<DrugTestViewModel>
而您需要它是List<ITestViewModel>
类型。 Since IQueryable<T>
is covariant, you can achieve that by simply specifying explicitly the desired type when calling ToList
like this 由于IQueryable<T>
是协变的,你可以通过简单地在调用ToList
时明确指定所需的类型来实现这一点。
var query = (...).ToList<ITestViewModel>();
The from at in context... is a query hence the IQuerable error Adding .ToList() call ad the end of your linq query returns a list that supports IEnumerable. from at in context ...是一个查询,因此IQuerable错误添加.ToList()调用ad,linq查询的结尾返回一个支持IEnumerable的列表。
Your first code snippet contains the .ToList() your second is missing. 您的第一个代码段包含.ToList(),您的第二个缺失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.