Sql – How to define a return List for a LinqToSql Join

join;linq-to-sql

I use Linq To Sql Join Query in DAL layer, How can I define a return List<T> automatically for the LinqToSql Join method?

Nowadays, I have to manually define a List<CustomViewType> for each LinqToSql Join method's return value.

Is that possible to define a List<T> like the following code :

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

In fact, what I mean is that how can I pass the lists of anonymous types from DAL layer to BLL layer ?

Best Answer

You must still create a custom class for the return type since you can't return anonymous classes from methods. You can avoid declaring and assigning the list though using the ToList() extension method:

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