带有数据对象的动态 LINQ

发布于 2024-09-29 01:59:11 字数 1559 浏览 1 评论 0原文

所以我一直在寻找一个简单的例子来解决一个简单的问题。

我有一个简单的对象,一个 ListTest (List) 列表,

public class ListTest{
{
    public string perName { get; set; }
    public string perSex { get; set; }
    public ListTest(string pName, string pSex)
    {
        this.perSex = pSex;
        this.perName = pName;
    }
}

我已经加载了一些数据:

        List<ListTest> tryIt = new List<ListTest>();
        tryIt.Add(new ListTest("Karen", "F"));
        tryIt.Add(new ListTest("Kate", "F"));
        tryIt.Add(new ListTest("Glen", "M"));
        tryIt.Add(new ListTest("Tiger", "M"));
        tryIt.Add(new ListTest("Clementine", "F"));
        tryIt.Add(new ListTest("Magnolia", "F"));

现在我想使用 Lambda 表达式来查询它:

        var things = tryIt
                     .Where(sex => (sex.perSex == "F"))
                     .OrderBy(sex => sex.perName);

但我想动态地执行此操作,以防万一我想更改我的位置“名字”。

我能够创建您的表达式的 Lambda 表达式,但我无法弄清楚如何将其跨越目标线并实际将其分配给 where 子句并执行它。

        IQueryable<ListTest> iq = tryIt.AsQueryable();
        ParameterExpression pe = Expression.Parameter(typeof(ListTest), "Person");
        Expression paEx = Expression.Property(pe, "perSex");
        Expression right = Expression.Constant("F");
        Expression eqEx = Expression.Equal(paEx, right);
        Expression lE = Expression.Lambda<Func<ListTest, bool>>(eqEx, pe);

这应该是一个简单的 4 或 5 行解决方案,但我找不到易于理解的解决方案示例。

我应该使用 MethodCallExpression 还是类似的东西?

谢谢,

So I have been searching for a simple example to hopefully a simple problem.

I have a simple object a List of ListTest (List)

public class ListTest{
{
    public string perName { get; set; }
    public string perSex { get; set; }
    public ListTest(string pName, string pSex)
    {
        this.perSex = pSex;
        this.perName = pName;
    }
}

I have loaded it with some data:

        List<ListTest> tryIt = new List<ListTest>();
        tryIt.Add(new ListTest("Karen", "F"));
        tryIt.Add(new ListTest("Kate", "F"));
        tryIt.Add(new ListTest("Glen", "M"));
        tryIt.Add(new ListTest("Tiger", "M"));
        tryIt.Add(new ListTest("Clementine", "F"));
        tryIt.Add(new ListTest("Magnolia", "F"));

Now I want to query it using a Lambda Expression:

        var things = tryIt
                     .Where(sex => (sex.perSex == "F"))
                     .OrderBy(sex => sex.perName);

But I want to do it dynamically, just in case I want to change my where to "perName".

I am able to create the Lambda Expression your Expressions, but I can't figure out how to take it across the goal line and actually assign it to a where clause and execute it.

        IQueryable<ListTest> iq = tryIt.AsQueryable();
        ParameterExpression pe = Expression.Parameter(typeof(ListTest), "Person");
        Expression paEx = Expression.Property(pe, "perSex");
        Expression right = Expression.Constant("F");
        Expression eqEx = Expression.Equal(paEx, right);
        Expression lE = Expression.Lambda<Func<ListTest, bool>>(eqEx, pe);

This should be a simple 4 or 5 line solution, but I can't find an easily decipherable solution example.

Should I use a MethodCallExpression or something down those lines?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轮廓§ 2024-10-06 01:59:11

您可以尝试这样的操作:

IEnumerable<ListTest> things = tryIt;

if (someBooleanLogic)
    things = things.Where(l => l.perSex == "F");
else
    things = things.Where(l => l.perName == "Tiger");

things = things.OrderBy(sex => sex.perName);

List<ListTest> filteredAndSorted = things.ToList();

编辑:

或者,还有流行的LINQ 动态查询库,您可以在其中几乎按照您想要的方式形成查询(当然是动态的) )。

You can try something like this:

IEnumerable<ListTest> things = tryIt;

if (someBooleanLogic)
    things = things.Where(l => l.perSex == "F");
else
    things = things.Where(l => l.perName == "Tiger");

things = things.OrderBy(sex => sex.perName);

List<ListTest> filteredAndSorted = things.ToList();

Edit:

Or, there's also the popular LINQ Dynamic Query Library, where you can form your query pretty much however you want (dynamically, of course).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文