views:

74

answers:

1

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 ?

+2  A: 

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();
}
Lee
Well, you *can* return lists of anonymous types... you just can't do so in a strongly typed way.
Jon Skeet
Then how to pass the lists of anonymous types from DAL layer to BLL layer?
Mike108