从EF select返回元组

.net c# entity-framework linq-to-entities

如何使用EF4在Select中检索元组?

        var productCount = (from product in context.products
                    select new Tuple<Product, int>(product, products.Orders.Count));

要么

        var productCount = (from product in context.products
                    select Tuple.Create(product, products.Orders.Count));

实体框架说第一种情况不能使用空构造函数,而第二种情况则不能识别Tuple.Create方法。

一般承认的答案

如何切换到投影的LINQ到对象:

var productCount = from product in context.products
                select new {Product = product, Count = products.Orders.Count };
var final = from item in productCount.AsEnumerable()
            select Tuple.Create(item.Product, item.Count);

热门答案

尝试

 var productCount = from product in context.products 
                    select new { product, Count = products.Orders.Count }; 

这将返回一个非常接近元组的匿名类型http://msdn.microsoft.com/en-us/library/bb738512.aspx



许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因