如何申请上LINQtoSQL结果的过滤器? [英] How to apply a filter on LINQtoSQL results?

查看:100
本文介绍了如何申请上LINQtoSQL结果的过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ListBox控件,可以给它一个DataSource,命名的DisplayMember和ValueMember,并通过一些魔法就会从数据源显示的字段,并返回一个选择ValueMember。它可以工作机智LINQ到SQL结果,甚至不知道具体谈谈它喂与表点儿。

With the ListBox control it is possible to feed it a DataSource, name a DisplayMember and a ValueMember and through some magic it will display a field from the DataSource and return a selected ValueMember. It can work wit a linq-to-sql result without even knowing anyting specific about the table it is feed with.

是不是反思和属性做一些魔法?它是如何工作的!
我有必要做类似的事情,但我不知道从哪里开始。我为LINQtoSQL初学者。

Isn't Reflection and Attributes doing some magic? How does it work! I have a need to do something similar but I do not know where to start. I'm a beginner for LINQtoSQL.

这是我想做的事情。
我有我要过滤源表。源表可以是任何东西,但会被一些DataContext的起源。

This is what I want to do. I have a source table that I want to filter. The source table can be anything but will be originating from some DataContext.

var MySourceTable =
    from MyRecord in Context.GetTable<MySourceTable>()
    select new
    {
        Value = MyRecord.ID,
        Display = MyRecord.Name,
        FilterValue = MyRecord.Value
    };

在我的控制我希望能够在某些给定值过滤MySourceTable。该控件不知道什么表用于(在上面的例子MySourceTable)和控制并不只知道它应该使用记录中的字段的三个名字,ID,名称和值。

In my control I want to be able to filter MySourceTable on some given value. The control does not know what table is used (MySourceTable in the example above) and the control does only know the three names, ID, Name and Value of the fields in the record it should use.

filter查询应该看起来像下面的例子。

The filter query should look like the example below.

var MyTable
    from Record in MySourceTable
    where FilterValue == GivenValue
    select new
    {
        Value = Record.ID,
        Display = Record.Name,
    };

有人可以告诉我在哪里开始呢?

Can somebody advise me on where to start?

推荐答案

我写了一个过滤引擎,在属性和值需要作为一个字符串,并能够使用它作为一个where子句。

I wrote a filter engine that takes in a Property and Value as a string, and is able to use that as a where clause.

IQueryable<T> FilterFunction<T>(IQueryable<T> query)
{
    ParameterExpression p = Expression.Parameter(typeof(T), "notused");

    Expression<Func<T, bool>> wherePredicate =
      Expression.Lambda<Func<T, bool>>(
          Expression.Equal(
            Expression.Call(Expression.Property(p, FilterProperty), "ToString", new Type[0]),
            Expression.Constant(FilterValue)), p);

    return query.Where(wherePredicate);
}

您应该能够传递一个防爆pression&LT; Func键&LT; T,TResult&GT;&GT; 建以同样的方式进 query.Select()

You should be able to pass in an Expression<Func<T, TResult>> built in a similar way into query.Select()

如果我理解正确你的问题,我相信这将工作:

If I am understanding your question correctly, I believe this will work:

string DisplayProperty = "Name";
string ValueProperty = "ID";

IQueryable<Record> SelectRecordProperties<T>(IQueryable<T> query)
{
    ParameterExpression p = Expression.Parameter(typeof(T), "notused");

    MethodInfo ctorMethod = typeof(Record).GetMethod("Create");

    Expression<Func<T, Record>> selectPredicate =
      Expression.Lambda<Func<T, Record>>(
        Expression.Call(ctorMethod,
            Expression.PropertyOrField(p, DisplayProperty),
            Expression.PropertyOrField(p, ValueProperty)), p);

    return query.Select(selectPredicate);
}
class Record
{
    public static Record Create(string display, string value)
    {
        return new Record() { Display = display, Value = value };
    }
    public object Display { get; set; }
    public object Value { get; set; }
}

因此​​,对于您的全功能你需要这两种想法,使您的过滤功能结合起来。

So for your full function you'd need to combine these two ideas so that your filtering works.

顺便说一句,有很多可能的方法来建立这个前pression树,有一些工具,我已经在一个点上发现了会告诉你前任pression树我想,这样你可以手动编写LINQ查询,看看怎样的.Net构建前pression,然后修改这个code将其基于为建设可能获得更高效的前pression树。

By the way, there are many possible ways to build the expression tree for this, there was some tool I've found at one point which would show you the expression tree I think, so you could manually write the linq query and see how .Net builds the expression, then modify this code to build it based on that to possibly get a more efficient expression tree.

这篇关于如何申请上LINQtoSQL结果的过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆