Sajjadd Hussain
Sajjadd Hussain

Reputation: 15

How to join two tables using LINQ?

i am trying to join two tables using the mvc4 web API and linq.

This is the class code for catagory table.

 public partial class catagory
{
    public int id { get; set; }
    public string cat_name { get; set; }
    public Nullable<int> measure_type { get; set; }
    public Nullable<int> active { get; set; }
    public Nullable<int> parent_cat { get; set; }
    public Nullable<int> display_order { get; set; }
    public string cat_image { get; set; }
}

code of the class for product table

public partial class product
{
    public int id { get; set; }
    public string p_name { get; set; }
    public Nullable<int> price { get; set; }
    public Nullable<int> catagory_id { get; set; }
    public Nullable<int> brand_id { get; set; }
    public Nullable<int> active { get; set; }
    public Nullable<int> discount_percent { get; set; }
    public Nullable<int> display_order { get; set; }
    public Nullable<int> color_id { get; set; }
    public Nullable<int> seller_id { get; set; }
    public int selling_price { get; set; }
}

I want to join these two table. here is the code in the ProductController.cs file

 public class ProductController : ApiController
{

    public List<product> GetProductByColour(int id)
    {
        var query = (from x in db.products.AsEnumerable()
                     join y in db.catagories.AsEnumerable()
                     on x.id equals y.id
                     where x.id.Equals(id)
                     select new product
                     {
                         id = x.id,
                         p_name = x.p_name,
                         price = x.price,
                         catagory_id = y.id,
                         brand_id = x.brand_id,
                         display_order = y.display_order,

                     }).ToList();
        return query.ToList();
    }

Upvotes: 0

Views: 12345

Answers (1)

Ashish Rajput
Ashish Rajput

Reputation: 1529

You should join both these table on products table catagory_id property and category table Id property, because in your schema only this one looks valid relation

And incoming variale Id can be any of these like Product Id, Category Id or may be color_id. For me it seems more color_id.

For more information on linq please follow this link

public class ProductController : ApiController
    {

        public List<product> GetProductByColour(int id)
        {
            var query = (from x in db.products.AsEnumerable()
                         join y in db.catagories.AsEnumerable()
                         on x.catagory_id equals y.id 
                         where x.id.Equals(id)
                         select new product
                         {
                             id = x.id,
                             p_name = x.p_name,
                             price = x.price,
                             catagory_id = y.id,
                             brand_id = x.brand_id,
                             display_order = y.display_order,

                         }).ToList();
            return query;
        }

Upvotes: 1

Related Questions