I am getting multiple rows of combination. I need Unique Vendor rows with unique VendorCod.
Code:
IQueryable<VendorModel> vendors = (from vendor in _context.Vendor
join vp in _context.VendorProduct on vendor.Pkid equals vp.VendorId
where vp.ProductId == ProductId
select new VendorModel()
{
VendorId = vendor.Pkid,
Name = vendor.Name,
VendorCode = vendor.VendorCode,
FacilityId = 0,
ClientId = 0,
Active = vendor.Active ?? false,
UpdatedAt = vendor.UpdatedAt,
DeletedAt = vendor.DeletedAt
});
You have many products for a vendors, so you have to apply appropriate predicate and no joins are needed.
Via navigation property:
var vendors =
from vendor in _context.Vendor
where vendor.Products.Any(vp => vp.ProductId == ProductId)
select new VendorModel
{
VendorId = vendor.Pkid,
Name = vendor.Name,
VendorCode = vendor.VendorCode,
FacilityId = 0,
ClientId = 0,
Active = vendor.Active ?? false,
UpdatedAt = vendor.UpdatedAt,
DeletedAt = vendor.DeletedAt
};
Or if you do not have navigation property:
var vendors =
from vendor in _context.Vendor
where _context.VendorProduct.Any(vp => vendor.Pkid == vp.VendorId && vp.ProductId == ProductId)
select new VendorModel
{
VendorId = vendor.Pkid,
Name = vendor.Name,
VendorCode = vendor.VendorCode,
FacilityId = 0,
ClientId = 0,
Active = vendor.Active ?? false,
UpdatedAt = vendor.UpdatedAt,
DeletedAt = vendor.DeletedAt
};
You will need to group your products and check if any match the code
IQueryable<VendorModel> vendors = (from vendor in _context.Vendor
join vp in _context.VendorProduct on vendor.Pkid equals vp.VendorId into vpg
where vpg.Any(vp => vp.ProductId == ProductId)
select new VendorModel()
{
VendorId = vendor.Pkid,
Name = vendor.Name,
VendorCode = vendor.VendorCode,
FacilityId = 0,
ClientId = 0,
Active = vendor.Active ?? false,
UpdatedAt = vendor.UpdatedAt,
DeletedAt = vendor.DeletedAt
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.