2013-12-16 2 views
0

WCF 서비스 라이브러리 및 Widnows Form을 클라이언트로 사용하고 있습니다. 데이터베이스가 있습니다. ADO.NET EF 해당 크기의 모든 제품 (옷)을 나열하고 싶습니다. (관계 1 대 다수). 내가 데이터베이스에이 해달라고WCF에서 WCF 서비스를받는 WCF

[DataContract] 
public class Products 
{ 
    [DataMember] 
    public int ID { get; set; } 
    [DataMember] 
    public string Name{ get; set; } 
    [DataMember] 
    public decimal Price { get; set; } 
    [DataMember] 
    public virtual ICollection<SizesEntity> Sizes{ get; set; } 

} 

[DataContract] 
public class Sizes 
{ 

    [DataMember] 
    public int ID { get; set; } 
    [DataMember] 
    public int Name { get; set; } 
    [DataMember] 
    public Nullable<int> Quantity { get; set; } 
    [DataMember] 
    public int ID_Product { get; set; } 

    [DataMember] 
    public virtual ProductsEntity Products { get; set; } 

} 

,하지만 난 내 쿼리 (그것을 다루는 그에게 임 확실하지 좋은 방법)

을 Products_with_sizes을 추가 :

public partial class ProductsEntity 
{ 
    public ProductsEntity() 
    { 
     this.Sizes = new HashSet<SizesEntity>(); 
    } 

    public int ID { get; set; } 
    public string Name { get; set; } 
    public decimal Price { get; set; } 

    public virtual ICollection<SizesEntity> Sizes{ get; set; } 
} 

이 내 데이터 계약이다

[DataContract] 
public class Products_with_sizes 
{ 
    [DataMember] 
    public int ID { get; set; } 
    [DataMember] 
    public string Name { get; set; } 
    [DataMember] 
    public decimal Price { get; set; } 
    [DataMember] 
    public int S { get; set; } 
    [DataMember] 
    public int M { get; set; } 
    [DataMember] 
    public int L { get; set; } 
} 

using (var context = new dbMagazynierEntities()) 
      { 
       var q = (from p in context.Products 
          where p.Name.Contains(name) && p.Price>= Price_from && p.Price <= Price_to 
          join r in context.Sizes 
           on p.ID equals r.Prodcuts.ID 
           into sizes 
          select new 
          { 
           ID = p.ID, 
           Name= p.Name, 
           Price = p.Price, 
           S = sizes.Where(x => x.Name== 0).Sum(x => x.Quantity) ?? 0, 
           M = sizes.Where(x => x.Name== 1).Sum(x => x.Quantity) ?? 0, 
           L = sizes.Where(x => x.Name== 2).Sum(x => x.Quantity) ?? 0, 
          }); 
       odp = new List<Products_with_sizes>(); 
       foreach (var item in q) 
       { 
        odp.Add(new Products_with_sizes{ ID = item.ID, Name= item.Name, Price = item.Price, S = item.S, M = item.M, L = item.L }); 
       } 

그래서 나는 내 고객에이 방법을 사용하여 알고 난 오류를

      wyn = context.SzukajProduktu(id, name.Text, price_from, price_to); 
를 얻을 수

내가 얻을 :

Cannot implicitly convert type 'System.Collections.Generic.List<Magazynier2WindowsFormsApplication.ServiceReference1.MyServiceProducts_with_sizes>' to 'System.Collections.Generic.List<Magazynier2ServiceLibrary.MyService.Products_with_sizes>' 
+0

[OperationContract] List<WCFLIB.MyService.Products_with_sizes> SzukajProduktu(int id, string name, decimal price_form, decimal price_to); 

을 변경하여 해결? 'context' 유형은 무엇입니까? 'SzukajProduktu' 메소드의 정의를 보여줍니다. –

답변

1

당신의 예외를 살펴보면, 당신이 직접 자신이 만든 DTO에 서비스 프록시에 의해 생성 된 클래스를 캐스팅하려는 것으로 보인다.

두 클래스가 동일한 이름과 속성을 가지고 있지만 실제로는 서로 다르며 (즉, 공통 부모 또는 인터페이스가 없음) 다른 네임 스페이스에 있습니다.

프록시 생성 클래스를 DTO 클래스로 명시 적으로 변환하는 메소드를 작성해야합니다.

List<Magazynier2ServiceLibrary.MyService.Products_with_sizes> TranslateProxyClassToDTO(List<Magazynier2WindowsFormsApplication.ServiceReference1.MyServiceProducts_with_sizes> input) 
{ 
    // translate all items and their properties and return the translated list 
} 
+0

나는 양식과 서비스 라이브러리 만 가지고 있습니다. 괜찮습니까? 또는 그들 사이에 레이어를 만들어야합니까? – johns

+1

꼭 할 필요는 없습니다. 중요한 것은 객체를 번역하는 것입니다. – Szymon

+0

번역본이 고객 또는 서비스에 있어야합니까? 나는 클라이언트에서 클래스 Products_with_sizes를 만들 수 있습니까? – johns

0
public List<Prodcuts_with_sizes> SzukajProduktu(int id, string name, decimal price_from, decimal price_to) 
    { 
     List<Prodcuts_with_sizes> odp; 
     if (id == -1) //when id is not given 
     { 
      using (var context = new dbMagazynierEntities()) 
      { 
       var q = (from p in context.Products 
          where p.Name.Contains(name) && p.Price >= price_from && p.Price <= price_to 
          join r in context.Size 
           on p.ID equals r.Products.ID 
           into sizes 
          select new 
          { 
           ID = p.ID, 
           Name = p.Name, 
           Price = p.Price, 
           S = sizes.Where(x => x.Name == 0).Sum(x => x.Quantity) ?? 0, 
           M = sizes.Where(x => x.Name == 1).Sum(x => x.Quantity) ?? 0, 
           L = sizes.Where(x => x.Name == 2).Sum(x => x.Quantity) ?? 0, 
          }); 
       odp = new List<Prodcuts_with_sizes>(); 
       foreach (var item in q) 
       { 
        odp.Add(new Prodcuts_with_sizes { ID = item.ID, Name = item.Name, Price = item.Price, S = item.S, M = item.M, L = item.L }); 
       } 
       //dataGridView1.DataSource = q.ToList(); 
      } 
      return odp; 
     } 
     else //when id is given 
     { 
      using (var context = new dbMagazynierEntities()) 
      { 
       var q = (from p in context.Products 
          where p.ID == id 
          join r in context.Sizes 
           on p.ID equals r.Products.ID 
           into sizes 
          select new 
          { 
           ID = p.ID, 
           Name = p.Name, 
           Price = p.Price, 
           S = sizes.Where(x => x.Name == 0).Sum(x => x.Quantity) ?? 0, 
           M = sizes.Where(x => x.Name == 1).Sum(x => x.Quantity) ?? 0, 
           L = sizes.Where(x => x.Name == 2).Sum(x => x.Quantity) ?? 0, 
          }); 
       odp = new List<Prodcuts_with_sizes>(); 
       foreach (var item in q) 
       { 
        odp.Add(new Prodcuts_with_sizes { ID = item.ID, Name = item.Name, Price = item.Price, S = item.S, M = item.M, L = item.L }); 
       } 
      } 
      return odp; 

     } 
    } 


using (var context = new MyInterfaceClient()) 
       { 
        wyn = context.SzukajProduktu(id, name.Text, price_from, price_to); 
        //return wyn; 
       } 
0

나는

당신이 코드의 마지막 조각에 대한 자세한 내용을 줄 수
[OperationContract] 
    List<MyService.Products_with_sizes> SzukajProduktu(int id, string name, decimal price_form, decimal price_to);