[英]Filter data using list of dynamic unique key value pairs linq/lamda expression
我有一个像下面的桌子
id speciesid value
--------------------------------
1 1 ABC
1 2 EDF
2 2 XYZ
3 1 PQR
从上表中,我需要基于唯一对过滤数据
例如:
说唯一对是类似{{1,ABC},{2,EDF}}
的数据,在对数据中,键是用于物种标识,而值是用于表中的值
所以预期的结果是
id speciesid value
--------------------------------
1 1 ABC
2 2 EDF
传入数据是一个键值对,它也是动态的,因此一次可以有两个或三个或n个对。
我已经在堆栈溢出中看到了这篇文章
但是它处理的是恒定数据对及其纯sql查询。
注意:我想到的一种解决方法是,创建一个列表并遍历各对,然后将结果添加到列表中。
另一个可能是我们可以在linq中使用contains
可能是其他选项o编写一个接受键值对并返回相应结果的函数
但是我相信这样做会有一个简单/正确的方法。
任何帮助将不胜感激。
编辑(上面的场景是一个示例,我在下面包含了真实的案例):
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;
}
类
public class SpeciesAdvancedSearchFieldRequest
{
public int MasterSpeciesCategoryId { get; set; }
public int EcoSystemTypeId { get; set; }
}
2级
public class SpeciesAdvancedSearchRequest:PaginationRequest
{
public ICollection<SpeciesAdvancedSearch> SpeciesAdvancedSearch { get; set; }
}
这就是我正在使用的代码。
las,您忘记命名表和参数了。 如果我谈论“您的输入表”和“您的输入参数”,这将是相当困难的,因此让我们定义它们:
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;}
}
显然,您有一系列Animals
,存储在称为table
东西table
。 您再次忘记提及表的类型。 我认为这是DBMS中的一个表,可以通过实体框架使用SQL进行访问。 可能是DbSet<Animal>
。 所以我想您输入以下内容:
IQueryable<Animal> animals = myDbContext.Animals;
IEnumerable<QueryParam> inputParameters = new QueryParam[]
{
new QueryParam() {SpeciesId = 1, Value = cow},
new QueryParam() {SpeciesId = 2, Value = cat},
};
现在,您希望所有SpeciesId
/ Value
组合等于输入参数至少一个元素的动物。
TODO:如果有几行SpeciesId == 1并且Value ==“ cow”,请定义您想要的内容。TODO:指定区分大小写。
var result = animals.Where(animal => inputParameters.Contains(new QueryParam()
{
SpeciesId = animal.SpeciesId,
Value = animal.Value,
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.