System.Linq.Dynamic:使用列表(IEnumerable)参数 [英] System.Linq.Dynamic : Working with list (IEnumerable) parameters

查看:51
本文介绍了System.Linq.Dynamic:使用列表(IEnumerable)参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 System.Linq.Dynamic 编写动态查询,但我不知道该如何将列表(IEnumerable)参数传递给查询: 这是我要实现的目标:

I am using System.Linq.Dynamic to be able to write dynamic queries but I could not figure it out that how I can pass list (IEnumerable) parameters to a query : Here's what I want to achieve :

SELECT * FROM People WHERE Role IN ('Employee','Manager')

这是同一个查询的Linq等效项:

And here's the Linq equivalent of the same query :

from person in People where (new string[]{"Employee","Manager"}).Contains(person.Role)

所以我心想,我可以使用动态Linq编写此查询:

So I thought to myself that I could write this query using dynamic Linq as :

People.Where("@0.Contains(Role)","(new string[]{\"Employee\",\"Manager\"})")

此版本也不起作用:

People.Where("(new string[]{"Employee","Manager"}).Contains(Role)")

所以这是一个问题:我如何应用Dynamic Linq库来处理列表和IEnumerable参数(如上述情况)?

So here's the question : How can I apply Dynamic Linq Library to be able to work with list and or IEnumerable parameters such as the above scenario ?

推荐答案

动态linq项目本身不支持包含",我有相同的要求,因此必须下载源代码并对其进行修改以支持它.

The Dynamic linq project does not support 'contains' natively, I had the same requirement and had to download the source and modify it to support it.

我已经失去了跟上任何nuget更新的能力,但是该解决方案现在可以满足我们的需求.我找不到我在哪里找到的,但这就是我做到的方式.

I have lost the ability to keep up with any nuget updates, but the solution now works for our needs. I can't find where I found this, but this is how I did it.

编辑dynamic.cs文件,并将以下内容添加到第566行附近:

Edit the dynamic.cs file and add the following to around line 566:

interface IEnumerableSignatures
{
    bool Contains(object selector); // Add this
    void Where(bool predicate);
    //...
// Then around line 628 add a new keyword:

static readonly string keywordOuterIt = "outerIt";
static readonly string keywordIt = "It";
//...
// above ParameterExpression It; add
ParameterExpression outerIt;

// In ParseIdentifier add
if (value == (object)keywordOuterIt) return ParseOuterIt();

//Then add that method
Expression ParseOuterIt()
{
    if (outerIt == null)
        throw ParseError(Res.NoItInScope);
    NextToken();
    return outerIt;
}

// In ParseAggreggate, add:
outerIt = it;

if (signature.Name == "Min" || signature.Name == "Max")
{
    typeArgs = new Type[] { elementType, args[0].Type };
}
else
{
    typeArgs = new Type[] { elementType };
}
if (args.Length == 0)
{
    args = new Expression[] { instance };
}
else
{   
    // add this section
    if (signature.Name == "Contains")
        args = new Expression[] { instance, args[0] };
    else
    {
        args = new Expression[] { instance, Expression.Lambda(args[0], innerIt) };
    }
}

// In CreateKeyWords()

d.Add(keywordOuterIt, keywordOuterIt); // Add this

我不知道我们是否可以在此处上传源代码,但是我一直在维护自己的Dynamic.cs副本,并尝试使其与nuget上的版本保持最新.如果您愿意,我很乐意上传.我只是不记得我从哪儿得到了所有信息,因为在Dynamic linq上搜索contains大多会产生错误的结果-指向字符串包含,而不是IEnumerable.contains.

I don't know if we can upload source here, but I've been maintaining my own copy of Dynamic.cs, and trying to keep it up to date with the version on nuget. I'll be happy to upload it if you want. I just don't remember where I got all this, because searching for contains on Dynamic linq mostly yields the wrong results - pointing to string contains, not IEnumerable.contains.

这篇关于System.Linq.Dynamic:使用列表(IEnumerable)参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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