我有一個WCF服務庫和Widnows窗體作爲客戶端。我有數據庫ADO.NET EF 我想列出所有產品(衣服)的大小。 (關係1到很多)。WCF從WCF服務獲取實體
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
{
[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我的查詢(林不知道它和它打交道的好方法)
[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>'
你能給你的最後一段代碼,更多的細節解決了嗎? 「上下文」是什麼類型?顯示'SzukajProduktu'方法的定義。 –