简体   繁体   中英

Linq to SQL attempting left join with DefaultIfEmpty() causing Unsupported overload used for query operator 'DefaultIfEmpty'. error

This is the first time I've tried doing a left join via linq, i have looked through stack overflow and Google but everything I have tried hasn't worked (quite possibly through my own lack of understanding). I am trying to do the following query:

IQueryable<MyType> pQ = (from prd in dc.ProductDatas 
join cc in dc.CategoryProducts.DefaultIfEmpty(defaultCP) 
on prd.ProductID equals cc.ProductID 
where cc.CatID == CatID
orderby cc.OrdWithinInCategory 
select prd);

I have defind defaultCP as:

CategoryProduct defaultCP = new CategoryProduct {ID = 1,CatID = CatID, OrdWithinInCategory = 999};

I am getting the following error:

Unsupported overload used for query operator 'DefaultIfEmpty'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: Unsupported overload used for query operator 'DefaultIfEmpty'.

Is there anything obvious I am doing wrong in my code, or do I need to try a different approach entirely. Any help much appreciated.

Use AsEnumerable().

CategoryProduct defaultCP = new CategoryProduct {ID = 1,CatID = CatID, OrdWithinInCategory = 999};

IQueryable<MyType> pQ = (from prd in dc.ProductDatas 
                        join cc in dc.CategoryProducts.DefaultIfEmpty(defaultCP) 
                           on prd.ProductID equals cc.ProductID 
                        where cc.CatID == CatID
                        orderby cc.OrdWithinInCategory 
                        select prd)
               .AsEnumerable()
               .DefaultIfEmpty(defaultCP).First();

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM