通用列表类型转换问题

发布于 2024-08-27 03:07:57 字数 1026 浏览 7 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦亿 2024-09-03 03:07:57

即使 Customer 继承自 EntityObjectList 也不会继承自 List,因为不支持泛型类型协变(在 C# 4.0 中,接口支持协变,但 IList 不支持)。

如果您可以将 List 分配给 List 类型的变量,它将允许您执行类似的操作:

List<EntityObject> list = new List<Customer>();
list.Add(new Product()); // assuming Product inherits from EntityObject

此代码显然已损坏:您无法将 Product 添加到 List。这就是为什么不允许

Even though Customer inherits from EntityObject, List<Customer> doesn't inherit from List<EntityObject>, because generic type covariance is not supported (in C# 4.0, covariance is supported for interfaces, but not for IList<T>).

If you could assign a List<Customer> to a variable of type List<EntityObject>, it would allow you to do something like that :

List<EntityObject> list = new List<Customer>();
list.Add(new Product()); // assuming Product inherits from EntityObject

This code is obviously broken : you can't add a Product to a List<Customer>. That's why it's not allowed

蓝戈者 2024-09-03 03:07:57
[OperationContract]
public List<EntityObject> GetData()
{
    using (TestEntities ctx = new TestEntities())
    {
        var data = from rec in ctx.Customer
                   select (EntityObject)rec;
        return data.ToList();
    }
}

即使 D 派生自 B,也不能将 List 转换为 List。这称为协方差,仅适用于数组。将在C# 4.0中全面引入

[OperationContract]
public List<EntityObject> GetData()
{
    using (TestEntities ctx = new TestEntities())
    {
        var data = from rec in ctx.Customer
                   select (EntityObject)rec;
        return data.ToList();
    }
}

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

能怎样 2024-09-03 03:07:57

您的 var 数据包含 Customer 对象,您的返回是包含 EntityObjects 的列表。

你必须投射它们:

data.ConvertAll(obj => (EntityObject) obj).ToList();

Your var data contains Customer objects, your return is a list with EntityObjects.

You'll have to cast them:

data.ConvertAll(obj => (EntityObject) obj).ToList();
顾忌 2024-09-03 03:07:57

你可以做

data.Cast<EntityObject>().ToList();

You could do

data.Cast<EntityObject>().ToList();
So要识趣 2024-09-03 03:07:57

除了 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文