join子句中某个表达式的类型不正确

时间:2014-03-10 19:29:39

标签: c# linq

我正在学习LINQ并尝试使用Cross join我收到此错误:join子句中某个表达式的类型不正确

在交叉连接中无法使用GetCategories()吗?这个请求是主要的,但是它以外的功能。

var q = from c in GetCategories()
        join p in GetProducts() on c equals p.CategoryID
        where c.Name = "Beverages"
        select new { ID = c, p.Name };

方法签名:

public static IEnumerable<Category> GetCategories()
List<Category> categories = new List<Category>( );
categories.Add( new Category { ID = 1, Name = "Beverages" } );

public static IEnumerable<Product> GetProducts()
List<Product> products = new List<Product>( );
products.Add( new Product { Name = "Milk",           Price = 90,  CategoryID = 4, ID = 1 } );

1 个答案:

答案 0 :(得分:2)

您需要指定要加入的Category课程中的哪个字段..很可能是ID

from c in GetCategories()
join p in GetProducts() on c.ID equals p.CategoryID
where c.Name == "Beverages"
select new { ID = c.ID, p.Name };