简体   繁体   中英

Filter data using list of dynamic unique key value pairs linq/lamda expression

I have a table like below

id  speciesid   value
--------------------------------
1   1       ABC
1   2       EDF
2   2       XYZ
3   1       PQR

From the above table I need to filter the data based on a unique pair

example :

say the unique pair is something like {{1,ABC},{2,EDF}} In the pair data the key is for speciesid and value is for value in the table

So the expected result would be

id  speciesid   value
--------------------------------
1   1       ABC
2   2       EDF

The incomming data is a keyvalue pair it is dynamic also, so two or three or n number of pair can come at a time.

I have seen this post in stack overflow

But it deals with constant pair of data and its pure sql query.

Note : One workaround in my mind is like create a list and loop through the pairs and add the results to the list.

The other one might be we can go with contains in linq

May be other option o write a function which accepts the the key value pair and return the corresponding results

But I believe there will be a simple/right approach for doing the same.

Any help will be appreciated.

Edit (the above scenario was a sample I have included the real case below):

 public SearchBase<Species> GetAdvancedSearchSpeciesList(SpeciesAdvancedSearchRequest request)
    {
        var ids = request.SpeciesAdvancedSearch.Select(o => o.FieldId).ToList();
        SearchBase<Species> searchResult = new SearchBase<Species>();
        List<Species> species = new List<Species>();
        var speciesList = _dbContext.Species.Join(_dbContext.SpeciesDetails,
           sp => sp.Id, sd => sd.SpeciesId, (sp, sd) => new { sp, sd })
           .Join(_dbContext.SpeciesFields, mapper => mapper.sd.SpeciesFieldId, sf => sf.Id, (mapper, sf) => new { mapper, sf })
           .Where(o =>
             ids.Contains(o.sf.Id)
           ).ToList();
        species = speciesList.Select(it => new Species()
        {
            CommonName = _resourceProvider.IsEnglish() ? it.mapper.sp.CommonNameEn : it.mapper.sp.CommonNameAr,
            ProfileImage = it.mapper.sp.ProfileImage,
            ScientificName = _resourceProvider.IsEnglish() ? it.mapper.sp.ScientificNameEn : it.mapper.sp.ScientificNameAr,
            SpeciesCategoryId = it.mapper.sp.SpeciesCategoryId,
            EcosystemTypeId = it.mapper.sp.EcosystemTypeId,
            Id = it.mapper.sp.Id,
            IUCNStatusId = it.mapper.sp.IUCNStatusId,
            LocalStatusId = it.mapper.sp.LocalStatusId
        }).ToList();
        var result = species.Where(sp => request.SpeciesAdvancedSearch.Contains(new SpeciesAdvancedSearch()
        {
           FieldId=sp.Id
        }));
// here the result count is 0
        int totalCount = speciesList.Count();
        request.Size = request.Size == 0 ? totalCount : request.Size;
        searchResult.Total = totalCount;
        searchResult.PageNumber = request.PageIndex;
        searchResult.AddRange(species.Skip((request.PageIndex - 1) * request.Size).Take(request.Size).ToList());
        searchResult.TotalItemsInPage = searchResult.Count;
        return searchResult;
    }

Class

public class SpeciesAdvancedSearchFieldRequest
{
    public int MasterSpeciesCategoryId { get; set; }

    public int EcoSystemTypeId { get; set; }
}

class 2

public class SpeciesAdvancedSearchRequest:PaginationRequest
{
    public ICollection<SpeciesAdvancedSearch> SpeciesAdvancedSearch { get; set; }

}

This is what the code I am using.

Alas you forgot to name your tables and parameters. It would be fairly difficult if I spoke about "your input table" and "your input parameters", so let's define them:

class Animal
{
    public int Id {get; set;}
    public int SpeciesId {get; set;}
    public string Value {get; set;}   // I haven't got a clue what value "2" would mean
}

class QueryParam
{
    public int SpeciesId {get; set;}
    public string Value {get; set;}
}

Apparently you have a sequence of Animals , stored in something you call table . Again you forgot to mentions the type of the table. I think it is a table in a DBMS that is accessed using SQL via entity-framework. Probably a DbSet<Animal> . So I guess you have the following input:

IQueryable<Animal> animals = myDbContext.Animals;
IEnumerable<QueryParam> inputParameters = new QueryParam[]
{
    new QueryParam() {SpeciesId = 1, Value = cow},
    new QueryParam() {SpeciesId = 2, Value = cat},
};

And now you want all animals that have a SpeciesId / Value combination equal to at least one element of your inputParameters.

TODO: define what you want if there are several rows with SpeciesId == 1 and Value == "cow" TODO: specify case sensitivity.

var result = animals.Where(animal => inputParameters.Contains(new QueryParam()
    {
          SpeciesId = animal.SpeciesId,
          Value = animal.Value,
    }));

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