2014-06-23 6 views
0

SQL 쿼리에서 모델 논리에 문제가 있습니다. 다음은이 프로젝트를 위해 만든 테이블입니다.m : n 관계에서 모델 가져 오기

처음

내 제품 표

public partial class Products 
{ 
    public Products() 
    { 
     this.Orders_Products = new HashSet<Orders_Products>(); 
     this.ShoppingCarts_Products = new HashSet<ShoppingCarts_Products>(); 
    } 

    public int ProductId { get; set; } 
    public string Name { get; set; } 
    public double Price { get; set; } 
    public int PPB { get; set; } 
    public int CategoryId { get; set; } 

    public virtual Categories Categories { get; set; } 
    public virtual ICollection<Orders_Products> Orders_Products { get; set; } 
    public virtual ICollection<ShoppingCarts_Products> ShoppingCarts_Products { get; set; } 
} 

그 다음은 shoppingCart 표

public partial class ShoppingCarts_Products 
{ 
    public int ShoppingCartId { get; set; } 
    public int ProductId { get; set; } 
    public int Count { get; set; } 

    public virtual Products Products { get; set; } 
    public virtual ShoppingCarts ShoppingCarts { get; set; } 
} 

가 지금은 모든 목록을 얻을 필요가 사이

public partial class ShoppingCarts 
{ 
    public ShoppingCarts() 
    { 
     this.ShoppingCarts_Products = new HashSet<ShoppingCarts_Products>(); 
    } 

    public int ShoppingCartId { get; set; } 
    public string UserId { get; set; } 
    public System.DateTime CreationDate { get; set; } 

    public virtual AspNetUsers AspNetUsers { get; set; } 
    public virtual ICollection<ShoppingCarts_Products> ShoppingCarts_Products { get; set; } 
} 

그리고 적어도 연결 표

내 ShoppingCart의 제품. 어떤 종류의 모델을 만들어야합니까?

필자는 SQL 문을 작성해야하는데,이 문은 메인 테이블과 연결 테이블을 병합합니다. 하지만 인터넷에서 어떤 옷을 입을 수는 없습니다. 아무도 샘플을 알고 있거나 그 문제를 해결하는 방법에 대한 힌트를 줄 수 있습니까?

이 단계가 끝나면 레이아웃 사이트에서 해당보기를 표시해야합니다.

그래, LINQ to SQL 형식으로 쿼리를 작성하려고합니다. 이제 레이아웃보기에서이보기를 부분보기로 표시하려고합니다.

@Html.Partial("_ShoppingCartPartial", Html.Action("MyShoppingCart","ShoppingCarts")) 

// GET: MyShoppingCart 
    public List<ShoppingCarts_Products> MyShoppingCart() 
    { 

     List<ShoppingCarts_Products> myproducts = db.ShoppingCarts_Products.Where(i => i.ShoppingCartId == 1).ToList();    

     return myproducts; 
    } 

감사

답변

0

당신은 시도 할 수는 :

또한
select * from ShoppingCarts_Products 
where ShoppingCarts_Products.ShoppingCartId = <variable> 
left join ShoppingCarts on ShoppingCarts.ShoppingCartId = ShoppingCarts_Products.ShoppingCartId 
left join Products on ShoppingCarts.ProductId = Products.ProductId 

, 당신이 필요로하는 필드 만 선택하려면 필드 선택 기준을 확장합니다.