【问题标题】:How to define a return List<T> for a LinqToSql Join?如何为 LinqToSql 连接定义返回 List<T>?
【发布时间】:2011-01-08 14:04:27
【问题描述】:

我在 DAL 层使用 Linq To Sql Join Query,如何为 LinqToSql Join 方法自动定义返回 List&lt;T&gt;

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

是否可以像以下代码一样定义List&lt;T&gt;

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 层?

【问题讨论】:

    标签: linq-to-sql join


    【解决方案1】:

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

    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();
    }
    

    【讨论】:

    • 嗯,你可以返回匿名类型的列表......你只是不能以强类型的方式这样做。
    • 那么如何将匿名类型列表从DAL层传递到BLL层呢?
    猜你喜欢
    • 2019-11-21
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多