Linq Contains in one request

I have a list and I want to write a query about the List ID. Contains a specific identifier for the table.

i Write this down and run true, but I want to write everything in one request.

List<int> tempList=yetkiUygulamaList.Select(y => y.Id).ToList();
query = query.Where(x => tempList.Contains(x.Uygulama.Id));

      

Bad request

query = query.Where(x => yetkiUygulamaList.Select(y =>y.Id).ToList().Contains(x.Uygulama.Id));

      

+3


source to share


3 answers


ToList () handles the request and after that NHibernate can't figure out that the first request should be included as a sub-request.

Just remove the useless ToList ():

IQueryable<int> tempList = yetkiUygulamaList.Select(y => y.Id);  // removed here
query = query.Where(x => tempList.Contains(x.Uygulama.Id));

      



The above code will generate one SQL query. If you want to put it all in one line of C # code, just get rid of the intermediate variable:

query = query.Where(x => yetkiUygulamaList.Select(y => y.Id).Contains(x.Uygulama.Id));

      

0


source


this should work



query = query.Where(x => yetkiUygulamaList.Any(y=>y.Id == x.Uygulama.Id));

      

0


source


you can do join , it would be simpler and more convenient in your case.

If I understand query

is the "collection" of the class (let's call it ) that contains the property and the class contains the property and is the "collection" AObj

Uygulama

Uygulama

Id

yetkiUygulamaList

Uygulama

//will return a IEnumerable<AObj>
IEnumerable<AObj> query = query.Join(yetkiUygulamaList, a => a.Uygulama.Id, u => u.Id, (a,u)=>a);

      

0


source







All Articles