开发者

Reusing part of the query with joins

I need to implement 3 public methods that all rely on the same initial join. Would a solution like the one described below work?

The code below is an example, the real stuff is much more complicated, that's why I want to reuse it.

private Tuple<Customer,开发者_StackOverflow中文版Order> GetCustomerOrders()
{
    from c in customers
    join o in orders
        on c.customerid equals o.customerid
    select Tuple.Create(c, o)
}

public MyCustomerOrder GetCustomerOrder(int customerId)
{
    return (from co in GetCustomerOrders()
    where co.Item1.customerid == customerId
    select new MyCustomerOrder(co.Item1, co.Item2)).FirstOrDefault();
}

public IEnumerable<MyCustomerOrder> GetCustomerOrders()
{
    return from co in GetCustomerOrders()
    orderby co.Item1.Name
    select new MyCustomerOrder(co.Item1, co.Item2);
}

The question is, does the tuple break the query? In other words, will this end up in the SQL query that gets generated where co.Item1.customerid == customerId?


It really depends on whether LINQ to SQL understands the point of Tuple.Create. I suspect that it does in .NET 4.0 - but the only way to find out is to try it.

It certainly makes conceptual sense, and composability is part of the goal of LINQ - which is why I'd hope it's supported. Effectively it's just like using an anonymous type, except you get to "export" the type information out of the method, which is the point of Tuple.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜