I'm running into some issues when constructing a query using LINQ and Entity Framework.
My model is like this
Merchants(MerchantId)
AffiliateFeeds(AffiliateFeedId, MerchantId)
ProductSKUs(ProductId, AffiliateFeedId)
This snippet works well enough:
// only get Merchants and AffiliateFeeds based on specific ProductSKU.ProductId
var productSKUs = from ps in _context.ProductSKUs
where ps.ProductId == 38
select ps.AffiliateFeedId;
var feeds = from af in _context.AffiliateFeeds
where productSKUs.Contains(af.AffiliateFeedId)
select af;
var dm = (from f in feeds select f.MerchantId).Distinct();
var merchants = from m in _context.Merchants
where dm.Contains(m.MerchantId)
select new
{
MerchantId = m.MerchantId,
Name = m.Name,
SysName = m.SysName,
DataFeedUrl = m.DataFeedUrl,
AffiliateFeeds = feeds.Where(x => x.MerchantId == m.MerchantId)
};
However when I try and perform the projection into an Entity Framework generated class called Merchant as so:
var merchants = from m in _context.Merchants
where dm.Contains(m.MerchantId)
select new Merchant
{
MerchantId = m.MerchantId,
Name = m.Name,
SysName = m.SysName,
DataFeedUrl = m.DataFeedUrl,
AffiliateFeeds = feeds.Where(x => x.MerchantId == m.MerchantId)
};
I get an error stating:
Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Objects.DataClasses.EntityCollection'