我有这个对象,我试图在C#中使用LINQ来应用过滤器,而我只是很好奇如何应用它。
public class glAccts
{
public string fund {get;set;}
public string location {get;set;}
public string costCenter {get;set;}
public string object {get;set;}
}
我想做的是可以有如下方法
public async TaskIEnumerable<<glAccts>> applyFilter(IEnumerable<glAccts> filterList)
{
... code to fetch a list of all glAccts -- glUserAccess --...
if (filteredGL.Count() > 0)
{
for( int i = 0; i < filteredGL.Count(); i++ )
{
if ( !string.IsNullOrEmpty(filteredGL.ElementAt(i).fund)) {
var response = glUserAccess.Where(c => c.fund.Contains(filteredGL.ElementAt(i).fund));
}
if ( !string.IsNullOrEmpty(filteredGL.ElementAt(i).location)) {
var response = glUserAccess.Where(c => c.location.Contains(filteredGL.ElementAt(i).location));
}
if ( !string.IsNullOrEmpty(filteredGL.ElementAt(i).costCenter)) {
var response = glUserAccess.Where(c => c.costCenter.Contains(filteredGL.ElementAt(i).costCenter));
}
if ( !string.IsNullOrEmpty(filteredGL.ElementAt(i).objects)) {
var response = glUserAccess.Where(c => c.objects.Contains(filteredGL.ElementAt(i).objects));
}
}
}
}
注意:这是我目前拥有的,但是我不确定如何合并我的响应,以及我的对象是否为空白该怎么办。我还假设使用LINQ可能会有更简单的方法。
我想做的是构造这样的东西:
SELECT glAccts WHERE (activeGlAccts.fund == filteredList[i].fund AND activeGlAccts.location == filteredList[i].location activeGlAccts.costCenter == filteredList[i].costCenter AND activeGlAccts.object == filteredList[i].object ) OR (activeGlAccts.fund == filteredList[i].fund AND activeGlAccts.location == filteredList[i].location activeGlAccts.costCenter == filteredList[i].costCenter AND activeGlAccts.object == filteredList[i].object )
注意:每个过滤的选项之间将存在一个OR,并且每个元素将按AND分组
尝试这个:
public async TaskIEnumerable<glAccts> applyFilter(IEnumerable<glAccts> filterList)
{
//... code to fetch a list of all glAccts -- glUserAccess --...
var response = glUserAccess.Where(c =>
filteredGL.Any(x=>
(string.IsNullOrEmpty(x.fund) || c.fund.Contains(x.fund)) &&
(string.IsNullOrEmpty(x.location) || c.location.Contains(x.location)) &&
(string.IsNullOrEmpty(x.costCenter) || c.costCenter.Contains(x.costCenter)) &&
(string.IsNullOrEmpty(x.objects) || c.objects.Contains(x.objects))
)
);
//... rest of code
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句