user3399655
user3399655

Reputation: 35

Type of one of the expressions in the join clause is incorrect

I´m learning LINQ and trying to use Cross join I get this error: Type of one of the expressions in the join clause is incorrect

Isn´t possible to use GetCategories() in Cross Join? This request is in main but the functions outside of it.

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

Method signatures:

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

Upvotes: 2

Views: 86

Answers (1)

Grant Winney
Grant Winney

Reputation: 66511

You need to specify which field in the Category class to join on.. most likely 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 };

Upvotes: 2

Related Questions