nam nam - 3 months ago 49
C# Question

LINQ query error on Inner Join

In my ASP.NET Core app, I'm getting the following error on the following query:

Error CS0119: 'products' is a type, which is not valid in the given context

NOTE: The above error is on the line

join prod in products on.....
of the following query

LINQ Query:

using System.Linq;
var innerJoinQuery =
from category in categories
join prod in products on category.ID equals prod.ID
select new { ProductName = prod.Name, Category = category.Name };


category:

public class category
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string categoryNumber { get; set; }
}


product:

public class product
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string productNumber { get; set; }
}


NOTE: This is a
code first
application that has the above model classes among others.

Answer
var db = new ProjNameDbContext();
var innerJoinQuery =
    from category in db.categories
    join prod in db.products on category.ID equals prod.CategoryID
    select new { ProductName = prod.Name, Category = category.Name };
Comments