mvc4 web APIとlinqを使用して2つのテーブルを結合しようとしています。LINQを使用して2つのテーブルを結合する方法は?
これは、Catagoryテーブルのクラスコードです。
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; }
}
製品テーブルのクラスのコード
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; }
}
私はこの二つのテーブルに参加したいです。ここ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();
}
に従ってください? –
実際にコードが実行され、return文の行にデバッガを置くと、 "Count = 0"として表示されます。 –
私は2つのテーブルに参加したい。これは私のクエリです –