简体   繁体   中英

Entity Framework with dynamic value set in where clause not working

My database and corresponding class structure is as par below

Class Helper:
   List<Util> UtilityInfo,
   
Class Util :
   int Id

I have filter array with below values

int[] idArray = { 1, 2, 3};

Expected query format:

 _dbContent.Helper.Where(hp => hp.utilityInfo.Any(ut => ut != null && ut.Util.id = 1 || ut => ut.Util.id == 2 || ut => ut.Util.id == 3));

I want to achieve same through looping on the id's array.

When I am execute above query it's work fine but want to construct same dynamically using loop.

I have tried with below pattern but it added some class level expression at [ut.Util.id == item] , observed the same inside Debug expression content in Visual Studio.

Finally because of which getting error at the time of retrieving data.

var helperInfo = new List<Helper>()

foreach(var item in idArray)
{
    helperInfo = helperInfo.Where(hp => hp.utilityInfo.Any(ut => ut != null && ut.Util.id == item));
}

Note : I have successfully done above with below pattern but want to achieve same through looping

helper= helper.Where
(
  hp => idArray.Any
  (
     uId => hp.utilityInfo!= null &&
     v.utilityInfo.Any
     (
        ut => ut.User != null &&
        ut.Id == uId
     )
  )).ToList();
}

Update 2

You can iterate over IQueryable by same concept in prev update like below or use expression

var helperInfo = _dbContent.Helper;
var filteredHelperInfo = new List<Helper>();

foreach(var item in idArray)
{
   filteredHelperInfo.AddRange(helperInfo.Where(hp => hp.utilityInfo.Any(ut => ut != null && ut.Util.id == item)).ToList());
}

If I now understand well if you need to query on the helper list using loop you need to create new list and add filtered items in it in each iteration like below

Note : in your code in first iteration your list will filtered with first id second time you'll not found any items in list with second id so your list will be empty

var helperInfo = new List<Helper>();
var filteredHelperInfo = new List<Helper>();

foreach(var item in idArray)
{
   filteredHelperInfo.AddRange(helperInfo.Where(hp => hp.utilityInfo.Any(ut => ut != null && ut.Util.id == item)));
}

Update

If I now understand well if you need to query on the helper list using loop you need to create new list and add filtered items in it in each iteration like below

Note : in your code in first iteration your list will filtered with first id second time you'll not found any items in list with second id so your list will be empty

var helperInfo = new List<Helper>();
var filteredHelperInfo = new List<Helper>();

foreach(var item in idArray)
{
   filteredHelperInfo.AddRange(helperInfo.Where(hp => hp.utilityInfo.Any(ut => ut != null && ut.Util.id == item)));
}

Old answer for minimal code

You can use contains instead of looping like below, this will get all items with the same ids in array you mentioned

helperInfo = helperInfo.Where(hp => hp.utilityInfo.Any(ut => ut != null && idArray.Contains( ut.Util.id)));

You just can use this

int[] idarray = { 1, 3, 5 };
var list = context.Helpers.Include(x => x.Utilities.Where(y => idarray.Contains(y.Id))).ToList();

Result

在此处输入图像描述

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