Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF - cannot convert from System.Linq.IQueryable to System.Data.Objects.ObjectQuery

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);
like image 678
GibboK Avatar asked Mar 04 '26 21:03

GibboK


1 Answers

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));
like image 113
Aducci Avatar answered Mar 07 '26 10:03

Aducci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!