简体   繁体   English

在C#中返回匿名类型的集合

[英]Returning collection of anonymous types in C#

I have a VS2010 solution consisting of two projects - a "data" project and a webservice project that consumes the data project. 我有一个VS2010解决方案,包含两个项目 - 一个“数据”项目和一个使用数据项目的Web服务项目。 In order to protect from exposing the schema of the database I've opted to return anonymous (var) objects to the consumer of the webservice. 为了防止暴露数据库的模式,我选择将匿名(var)对象返回给webservice的使用者。 My question is, some of what I return will be in collections. 我的问题是,我返回的一些内容将在收藏中。 So instead of using this code to return a single anonymous object: 因此,而不是使用此代码返回单个匿名对象:

var item = from w in db.Widgets
    where w.widget_id == 1
    select new {
        name = w.name, 
        address = w.address
    };

I would want to use something similar to this to return a collection. 我想使用类似的东西来返回一个集合。

IQueryable<var> item = (from w in db.Widgets
        where w.widget_id == 1
        select new {
            name = w.name, 
            address = w.address
        }).IQueryable;

I realize this is not the exact way to do this ... just need to know how it would really be done. 我意识到这不是确切的方法......只需要知道它将如何实现。

Thanks! 谢谢!

Anonymous types themselves cannot be exposed outside of their declaring function, so the only way to expose an instance of an anonymous type outside of the declaring function is as an object . 匿名类型本身不能在其声明函数之外公开,因此在声明函数之外公开匿名类型实例的唯一方法是作为object If you need to do this, you'll have to declare a type at a higher scope with the properties that you need, then hydrate it within your query rather than the anonymous type. 如果需要这样做,则必须使用所需的属性在更高的范围内声明类型,然后在查询中而不是匿名类型中对其进行水合。

Instead of returning anonymous types, I'd have a strong typed layer that is returned. 我没有返回匿名类型,而是返回了一个强类型层。 It can still be filled from the database in whichever way you want and you'd have a strong contract for the consumer of the service. 它仍然可以以您想要的任何方式从数据库中填充,并且您与服务的消费者签订了强有力的合同。 The consumer won't even know there is a database, it could just as well be xml. 消费者甚至不知道有数据库,它也可能是xml。

You could, in theory, do this: 理论上,你可以这样做:

public List<object> GetObjects()
{
    return (from w in db.Widgets
        where w.widget_id == 1
        select (object) new {
            name = w.name, 
            address = w.address
        }).ToList();
}

However, I don't understand why you want to do this — the caller will obviously not be able to make any use of your anonymous type, except via Reflection or other hacky stuff. 但是,我不明白你为什么要这样做 - 调用者显然无法使用你的匿名类型,除非通过Reflection或其他hacky东西。 Why not simply return a normal class that everyone can use normally? 为什么不简单地返回一个每个人都能正常使用的普通课程?

public class NameAndAddress
{
    public string Name { get; private set; }
    public string Address { get; private set; }
    public NameAndAddress(string name, string address)
    {
        Name = name;
        Address = address;
    }
}

public List<NameAndAddress> GetObjects()
{
    return (from w in db.Widgets
        where w.widget_id == 1
        select new NameAndAddress(w.name, w.address)
    ).ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM