简体   繁体   English

使用linq或c#过滤Datatable动态/多重条件?

[英]Filter Datatable dynamic/multiple condition using linq or c#?

I have datatable dtResult and it's columns are 我有数据表dtResult,它的列是

BuildSequence,
Build#, 
BOptions,
BJob, 
LogID BType, 
BTypeKey, 
BComponentName, 
BCKey, 
RName, 
Rkey, 
RDescrip, 
RStatus, 
BuildMatchExp,
VPart, 
PNumber, 
SName, 
SKey, 
OName, 
OKey, 
Date

Now i have few filters, these filters are created dynamically and then user can select those filters. 现在我有几个过滤器,这些过滤器是动态创建的,然后用户可以选择那些过滤器。 These filters are the columns names of dtResult. 这些过滤器是dtResult的列名称。 I have list of selected filters and list contains the FilterName and Its Value. 我有选定过滤器的列表,列表包含FilterName及其值。 my question is based on Selected filters how i can filter dtResult? 我的问题是基于选定的过滤器,我如何过滤dtResult? As these filters are not constant they are changing every time. 由于这些滤波器不是恒定的,因此每次都在变化。 Changing every time i mean users are allowed to select any Filter. 每次更改都意味着用户可以选择任何过滤器。

eg; 例如; in one situation user can select BTypeKey and/or BCKey and/or Rkey in-short they can select any filter or no filter. 在一种情况下,用户可以简短地选择BTypeKey和/或BCKey和/或Rkey,他们可以选择任何过滤器或不选择过滤器。

Now i can filter dResult based on "Fixed" Column but i am not sure how i can do filtration on dynamic filters? 现在,我可以基于“固定”列过滤dResult,但是我不确定如何对动态过滤器进行过滤? Or How i can use linq to do it? 或我如何使用linq来做到这一点?

Any Help will be really appreciated, as i am struggling with it. 任何帮助将不胜感激,因为我一直在努力。

At code side so far i have just FilterList as this 到目前为止,在代码方面,我只有FilterList这样

FilterParameters filterlist = new FilterParameters(); 
filterlist.Add(new FilterParameter(this._BuildSequence, this._BuildSequenceName, FilterParameterTypes.Guid, FilterComparisonTypes.BuildSequence)); 
filterlist.Add(new FilterParameter(this._BCKey, this._BComponentName, FilterParameterTypes.Guid, FilterComparisonTypes.BCKey)); 
filterlist.Add(new FilterParameter(this._BTypeKey, base.BTypeName, FilterParameterTypes.Guid, FilterComparisonTypes.BTypeKey)); 
return filterlist; 

Thanks 谢谢

I think the way I'd approach this would be to have Filter objects that act on the IQueryable returned by the base query. 我认为我要采用的方法是让Filter对象对基础查询返回的IQueryable起作用。 Then map the Filter objects to the filters selected by users. 然后将“过滤器”对象映射到用户选择的过滤器。

For instance: 例如:

class SomeDbContext : DbContext
{
    public IDbSet<Person> People { get; set; }
}

class Person
{
    public string Foreame { get; set; }
    public string Surname { get; set; }
    public int Age { get; set; }
}

abstract class PersonFilter
{
    public abstract IQueryable<Person> Filter(IQueryable<Person> query, string value);
}

class PersonForenameFilter : PersonFilter
{
    public override IQueryable<Person> Filter(IQueryable<Person> query, string value)
    {
        return query.Where(t => t.Foreame == value);
    }
}

class PersonAgeFilter : PersonFilter
{
    public override IQueryable<Person> Filter(IQueryable<Person> query, string value)
    {
        return query.Where(t => t.Age.ToString() == value);
    }
}

Then you can create a mapping using say a Dictionary between a friendly label selectable by the user and the Filter object. 然后,您可以在用户可以选择的友好标签和Filter对象之间使用“字典”创建映射。 Then you just do: 然后,您只需执行以下操作:

string filterValue = "Dave";
var someFilter = new PersonForenameFilter();
var baseQuery = dbContext.People;
var filteredQuery = someFilter.Filter(baseQuery, filterValue);

Where someFilter is actually retrieved from your mapping dictionary. 实际上是从映射字典中检索someFilter的位置。

Also, note that this is composable so you could apply more than one filter. 另外,请注意,这是可组合的,因此您可以应用多个过滤器。

Thanks @MethodMan, your DataTable.Select Method did work and i converted my Filterlist to expression by using 感谢@MethodMan,您的DataTable.Select方法确实可以正常工作,我通过使用将我的Filterlist转换为表达式

var a = filterList.GroupBy(g => g.ParameterName).Select(gr => 
    string.Format("{0}={1}", gr.First().ParameterName, string.Join(",", gr.Select(g => g.Value))));
var b = string.Join(" And ", a.ToArray());

However, I am still stick to passing SQL Parameters to a stored procedure because one of the parameters is csv (and i have used Split for csv at database end) and expression for Select method has to be like ID=1 and Name='abc' and Address='here' and city='there' but if you want Name to be 'abc' or 'xyz' you need ".. and Name='abc' or Name='xyz' but its comma separated value for me like Name={'abc,xyz'} and I wasn't able to convert Name to Name='abc' or Name='xyz' . 但是,我仍然坚持将SQL参数传递给存储过程,因为其中一个参数是csv(并且我在数据库端使用了Split作为csv的对象),并且Select方法的表达式必须像ID=1Name='abc' and Address='here'city='there'但如果您希望Name为'abc'或'xyz',则需要“ .. and Name='abc'Name='xyz'但逗号分隔值我喜欢Name={'abc,xyz'} ,但无法将Name转换为Name='abc'Name='xyz'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM