[英]WCF getting entity from WCF service
I have a WCF Service Library and Widnows Form as a client. 我有一个WCF服务库和Widnows表单作为客户端。 I have database ADO.NET EF I want to list all of the products (clothes) with their sizes. 我有ADO.NET EF数据库,我想列出所有产品(衣服)及其尺寸。 (Relation 1 to many). (关系1至许多)。
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; }
}
this is my data contract: 这是我的数据合同:
[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; }
}
i dont have this in data base, but i added Products_with_sizes for my query (Im not sure its a good way of dealing with it) 我在数据库中没有此功能,但是我为查询添加了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 });
}
so know I use this method in my client and i get error 所以知道我在客户中使用这种方法会出现错误
wyn = context.SzukajProduktu(id, name.Text, price_from, price_to);
i get: 我得到:
Cannot implicitly convert type 'System.Collections.Generic.List<Magazynier2WindowsFormsApplication.ServiceReference1.MyServiceProducts_with_sizes>' to 'System.Collections.Generic.List<Magazynier2ServiceLibrary.MyService.Products_with_sizes>'
By looking at your exception, it seems that you're trying to directly cast a class generated by your service proxy to the DTO you created yourself. 通过查看异常,您似乎正在尝试将由服务代理生成的类直接转换为您自己创建的DTO。
Even though those 2 classes have the same name and properties, they are in fact different (ie have no common parent or inteface) and are in a different namespace. 即使这两个类具有相同的名称和属性,但它们实际上是不同的(即,没有共同的父代或接口),并且位于不同的命名空间中。
You should write a method that would translate the proxy generated class to your DTO class explicitely, eg 您应该编写一个将代理生成的类显式转换为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
}
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;
}
I resolved it by changing 我通过更改解决了
[OperationContract]
List<WCFLIB.MyService.Products_with_sizes> SzukajProduktu(int id, string name, decimal price_form, decimal price_to);
to 至
[OperationContract]
List<MyService.Products_with_sizes> SzukajProduktu(int id, string name, decimal price_form, decimal price_to);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.