简体   繁体   English

如何使用LINQ连接两个表?

[英]How to join two tables using LINQ?

i am trying to join two tables using the mvc4 web API and linq. 我正在尝试使用mvc4 Web API和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 这是ProductController.cs文件中的代码

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

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 您应该将这两个表同时连接到products表的catagory_id属性和category表的Id属性,因为在您的模式中,只有这一个看起来有效的关系

And incoming variale Id can be any of these like Product Id, Category Id or may be color_id. 传入的variale ID可以是产品ID,类别ID中的任何一个,也可以是color_id。 For me it seems more color_id. 对我来说,似乎更多的是color_id。

For more information on linq please follow this link 有关linq的更多信息,请点击此链接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM