Reputation: 81
I want to Convert the data that searched into a model since it split because I'm using repository pattern. this is my code:
public List<Supplier> Find(string name)
{
using (var suppre = new SupplierRepository())
{
return suppre.Find(x => x.Supplier_Name == name).ToList().Select(x => new SupplierView()
{
Supplier_Id = x.Supplier_Id,
Supplier_Name = x.Supplier_Name,
Supplier_Address = x.Supplier_Address,
Email = x.Email,
Contact_No = x.Contact_No,
Contact_Person = x.Contact_Person,
Type_of_medicine = x.Type_of_medicine,
Product_ID = x.Product_ID
});
}
}
and it gives me an error saying:
Cannot convert expression type
System.Collections.Generic.List<Dept.Model.SupplierModel>
to return typeSystem.Collections.Generic.List<Dept.Data.Supplier>
Upvotes: 0
Views: 176
Reputation: 1
You should try this:
public List<SupplierView> Find(string name)
{
using (var suppre = new SupplierRepository())
{
return suppre.Find(x => x.Supplier_Name == name).Select(x => new SupplierView()
{
Supplier_Id = x.Supplier_Id,
Supplier_Name = x.Supplier_Name,
Supplier_Address = x.Supplier_Address,
Email = x.Email,
Contact_No = x.Contact_No,
Contact_Person = x.Contact_Person,
Type_of_medicine = x.Type_of_medicine,
Product_ID = x.Product_ID
}).ToList();
}
}
the reason why it gave you an error was because your method signature stated you returning a List which is your data and you first have to do Select which is conversion statement before you return it to list.
Upvotes: 0
Reputation: 5487
In addition to what @mreyeros said, you will also need to move your ToList
as Select
returns a IEnumerable
.
public List<SupplierView> Find(string name)
{
using (var suppre = new SupplierRepository())
{
return suppre.Find(x => x.Supplier_Name == name).Select(x => new SupplierView()
{
Supplier_Id = x.Supplier_Id,
Supplier_Name = x.Supplier_Name,
Supplier_Address = x.Supplier_Address,
Email = x.Email,
Contact_No = x.Contact_No,
Contact_Person = x.Contact_Person,
Type_of_medicine = x.Type_of_medicine,
Product_ID = x.Product_ID
}).ToList();
}
}
Upvotes: 0
Reputation: 12429
I applaud your use of separate data models from edit models. I would recommend using a tool like AutoMapper to make some of this a lot easier (and automatic).
With automapper your method would look something like this:
public IEnumerable<SupplierView> Find(string name)
{
using (var suppre = new SupplierRepository())
{
return suppre
.Where(x => x.Supplier_Name == name)
.Select(x => AutoMapper.Mapper.Map<SupplierView>(x));
}
}
Upvotes: 0
Reputation: 4379
Your method signature is expecting to return a
List<Supplier>
but your actual return statement from your linq select is returning a
List<SupplierView>
you may simply need to change your return type to be
List<SupplierView>
Upvotes: 2