首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何定义LinqToSql Join的返回List<T>?

如何定义LinqToSql Join的返回List<T>?
EN

Stack Overflow用户
提问于 2010-01-24 00:03:00
回答 1查看 1.6K关注 0票数 1

我在DAL层使用Linq To Sql Join查询,如何为LinqToSql Join方法自动定义一个返回List<T>

现在,我必须为每个LinqToSql Join方法的返回值手动定义一个List<CustomViewType>

是否可以定义如下代码所示List<T>

代码语言:javascript
复制
public static List<T> GetJoinList()
{
    List<T> list = new List<T>();

    list =  from c in customers
            join o in orders on o.customerid equals c.customerid
            select new { CustomerID = c.CustomerId, OrderDate = o.OrderDate } ;

    return list.ToList() ;
}

实际上,我的意思是如何将匿名类型列表从DAL层传递到BLL层?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-01-24 00:09:07

您仍然必须为返回类型创建自定义类,因为您不能从方法返回匿名类。通过使用ToList()扩展方法,您可以避免声明和分配列表:

代码语言:javascript
复制
public static List<CustomViewType> GetJoinList()
{
    return (from c in customers
            join o in orders on o.customerid equals c.customerid
            select new CustomViewType { CustomerID = c.CustomerId, OrderDate = o.OrderDate}).ToList();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2123737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档