Reputation: 73998
I need subtract the result of contentCategories
from allCategories
at the moment I use .Except
method but I receive an error.
Any idea what I'm doing wrong... many thanks
Error 2 Argument 1: cannot convert from 'System.Linq.IQueryable<System.Data.Objects.DataClasses.EntityCollection<WebProject.DataAccess.DatabaseModels.CmsCategory>>' to 'System.Data.Objects.ObjectQuery<WebProject.DataAccess.DatabaseModels.CmsCategory>'
int contentId = Int32.Parse(uxContentsListSelector.SelectedValue);
var allCategories = from category in context.CmsCategories select category;
var contentCategories = from content in context.CmsContents
where content.ContentId == contentId
select content.CmsCategories;
var result = context.CmsCategories.Except(contentCategories);
Upvotes: 3
Views: 5436
Reputation: 26694
You need to add an additional from in
clause in order to get a flattened list.
var contentCategories = from content in context.CmsContents
from cat in content.CmsCategories
where content.ContentId == contentId
select cat;
I would then change your Except
method to an Any
method
var result = context.CmsCategories.Where(cat => !contentCategories.Any(c => c.CategoryId == cat.CategoryId));
Upvotes: 3
Reputation: 7458
That's because of the last select you're doing. Also you're code is highly reduntant. Let's take it by steps:
var allCategories = from category in context.CmsCategories select category;
You can rewrite the same in a more concise way:
var allCategories = context.CmsCategories;
Then this is wrong: you're selectin a query of collections instead of a single collection:
var contentCategories = from content in context.CmsContents
where content.ContentId == contentId
select content.CmsCategories;
The correct way to do that is:
var contentCategories = (from content in context.CmsContents
where content.ContentId == contentId
select content).First().CmsCategories;
Which is, again, redundant, and can be written in a better way:
var contentCategories = context.CmsContents.First(c => c.ContentId == contentId).CmsCategories;
The last statement is right.
Upvotes: 2