Reputation: 183
I have a WCF Service Library and Widnows Form as a client. I have database ADO.NET EF I want to list all of the products (clothes) with their sizes. (Relation 1 to many).
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)
[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>'
Upvotes: 0
Views: 394
Reputation: 183
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);
Upvotes: 0
Reputation: 183
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;
}
Upvotes: 0
Reputation: 43023
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.
Even though those 2 classes have the same name and properties, they are in fact different (i.e. 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, e.g.
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
}
Upvotes: 1