通用列表类型转换问题
我是 C# 新手,并且坚持以下内容。我有一个 Silverlight Web 服务,它使用 LINQ 查询 ADO.NET 实体对象。例如:
[OperationContract]
public List<Customer> GetData()
{
using (TestEntities ctx = new TestEntities())
{
var data = from rec in ctx.Customer
select rec;
return data.ToList();
}
}
这很好,但我想做的是让它更抽象。第一步是返回一个 List
但这会产生编译器错误,例如:
[OperationContract]
public List<EntityObject> GetData()
{
using (TestEntities ctx = new TestEntities())
{
var data = from rec in ctx.Customer
select rec;
return data.ToList();
}
}
错误是:
Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<SilverlightTest.Web.Customer>' to 'System.Collections.Generic.IEnumerable<System.Data.Objects.DataClasses.EntityObject>'. An explicit conversion exists (are you missing a cast?)
我做错了什么?
谢谢,
阿杰
I'm new to C# and am stuck on the following. I have a Silverlight web service that uses LINQ to query a ADO.NET entity object. e.g.:
[OperationContract]
public List<Customer> GetData()
{
using (TestEntities ctx = new TestEntities())
{
var data = from rec in ctx.Customer
select rec;
return data.ToList();
}
}
This works fine, but what I want to do is to make this more abstract. The first step would be to return a List<EntityObject>
but this gives a compiler error, e.g.:
[OperationContract]
public List<EntityObject> GetData()
{
using (TestEntities ctx = new TestEntities())
{
var data = from rec in ctx.Customer
select rec;
return data.ToList();
}
}
The error is:
Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<SilverlightTest.Web.Customer>' to 'System.Collections.Generic.IEnumerable<System.Data.Objects.DataClasses.EntityObject>'. An explicit conversion exists (are you missing a cast?)
What am i doing wrong?
Thanks,
AJ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
即使
Customer
继承自EntityObject
,List
也不会继承自List
,因为不支持泛型类型协变(在 C# 4.0 中,接口支持协变,但IList
不支持)。如果您可以将
List
分配给List
类型的变量,它将允许您执行类似的操作:此代码显然已损坏:您无法将
Product
添加到List
。这就是为什么不允许Even though
Customer
inherits fromEntityObject
,List<Customer>
doesn't inherit fromList<EntityObject>
, because generic type covariance is not supported (in C# 4.0, covariance is supported for interfaces, but not forIList<T>
).If you could assign a
List<Customer>
to a variable of typeList<EntityObject>
, it would allow you to do something like that :This code is obviously broken : you can't add a
Product
to aList<Customer>
. That's why it's not allowed即使 D 派生自 B,也不能将 List 转换为 List。这称为协方差,仅适用于数组。将在C# 4.0中全面引入
you can't cast List to List even if D derives from B. It is called covariance and works with arrays only. It will be fully introduced in C# 4.0
您的 var 数据包含 Customer 对象,您的返回是包含 EntityObjects 的列表。
你必须投射它们:
Your var data contains Customer objects, your return is a list with EntityObjects.
You'll have to cast them:
你可以做
You could do
除了 data.Cast().ToList(); 之外,您可能需要将派生类型定义为已知类型,以便 WCF 知道如何序列化派生对象。
http://msdn.microsoft.com/en-us/library/ms730167。 ASPX
Beyond the
data.Cast<EntityObject>().ToList();
, you will probably need to define the derived types on as know types, so WCF will know how to serialize the derived objects.http://msdn.microsoft.com/en-us/library/ms730167.aspx