Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator

后端 未结 4 584
夕颜
夕颜 2020-11-29 08:29

I am using LINQ in my project and my code is:

var SE = from c in Shop.Sections
                    join c1 in obj.SectionObjects on c.SectionId equals c1.Sec         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 08:55

    You can't use a Join between a SQL source and a local source. You'll need to bring the SQL data into memory before you can join them. In this case, you're not really doing a join since you only take the elements from the first collection, what you want is a select...where...selectid in query, which you can get using the Contains method.

     var SE = Shop.Sections.Where( s => obj.SectionObjects
                                           .Select( so => so.SectionId )
                                           .Contains( s.SectionId ))
                           .ToList();
    

    Translates to

    select * from Sections where sectionId in (...)
    

    where the values for the in clause come from the list of ids in the collection of local objects.

提交回复
热议问题