动态 Linq 和未知数量的替换值?

发布于 2024-09-27 16:21:55 字数 324 浏览 7 评论 0原文

通常,带有字符串的动态 linq 查询可以使用替换值,例如:

result =
    db.Persons.Where("Name == @1", "John");

我想要将未知数量的字符串传递到 Where 子句中。我对整数没有问题,但 API 似乎无法处理没有替换值的字符串。

有谁知道解决这个问题的方法吗?我为 Where 语句创建了一个串联字符串,因此我可以添加“@1”或其他内容,但我无法向 Where() 添加参数,所以我陷入了困境。

谢谢!

Typically a dynamic linq query with string can use a substitution value such as:

result =
    db.Persons.Where("Name == @1", "John");

I have an unknown number of strings that I want to pass into the Where clause. I have no problem with integers, but the API cannot seem to handle a string without a substitution value.

Does anyone know a way around this? I created a concatenated string for my Where statement, so I can add "@1" or whatever, but I cannot add parameters to the Where() so I am stuck.

Thanks!

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

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

发布评论

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

评论(3

猫烠⑼条掵仅有一顆心 2024-10-04 16:21:55

我为 where 语句创建了一个串联字符串,因此我可以添加“@1”或其他内容,但我无法向Where() 添加参数,所以我陷入了困境。

是的,你可以。 Where 方法的第二个参数是 params object[]values,因此您只需传递一个对象数组即可。

例如,假设您在字典中拥有属性名称和值,您可以执行以下操作:

var dic = new Dictionary<string, object>
{
    { "Name", "John" },
    { "Age", 30 },
    { "City", "New York" }
};

...

var conditions = dic.Keys.Select(
    (key, idx) =>
        string.Format("{0} == @{1}", key, idx));
string predicate = string.Join(" And ", conditions);
object[] values = dic.Values.ToArray();
result = db.Persons.Where(predicate, values);

I creating a concatenated string for my where statement, so i can add "@1" or whatever, but I cannot add parameters to the Where() so I am stuck.

Yes, you can. The second argument of the Where method is params object[] values, so you just need to pass an array of objects.

For instance, assuming you have the property names and values in a dictionary, you could do something like this:

var dic = new Dictionary<string, object>
{
    { "Name", "John" },
    { "Age", 30 },
    { "City", "New York" }
};

...

var conditions = dic.Keys.Select(
    (key, idx) =>
        string.Format("{0} == @{1}", key, idx));
string predicate = string.Join(" And ", conditions);
object[] values = dic.Values.ToArray();
result = db.Persons.Where(predicate, values);
梦开始←不甜 2024-10-04 16:21:55

我想我明白你关于字符串替换问题的意思。

这里有几个可供探索的替代方案。 Thomas Petricek 的解决方案(如果您能理解的话)特别有趣:

在运行时用 C# 构建[动态] LINQ 查询
http://tomasp.net/articles/dynamic-linq-queries.aspx

使用 PredicateBuilder 动态组合表达式谓词
http://www.albahari.com/nutshell/predicatebuilder.aspx


另请参阅http://blogs.msdn.com/b/mattwar/archive /2006/05/10/594966.aspx

I think I can see what you mean about the string substitution problem.

Here are a couple of alternatives to explore. Thomas Petricek's solution, if you can follow it, is especially interesting:

Building [Dynamic] LINQ Queries at Runtime in C#
http://tomasp.net/articles/dynamic-linq-queries.aspx

Dynamically Composing Expression Predicates using PredicateBuilder
http://www.albahari.com/nutshell/predicatebuilder.aspx


See also http://blogs.msdn.com/b/mattwar/archive/2006/05/10/594966.aspx

好多鱼好多余 2024-10-04 16:21:55

我为此做了一些事情。将 David Fowlers DynamicLinq 项目与 PredicateBuilder 结合使用,您可以从 Generic IQueryable 构建谓词并构建它们。特别感谢 StackOverflow 的一些答案,它给了我这一行从

Func<dynamic, dynamic>

to:

Expression<Func<T, bool>>

实现转换。

private static Expression<Func<T, bool>> GetFuncTbool<T>(IQueryable source, Func<dynamic, dynamic> expressionBuilder)
{
    ParameterExpression parameterExpression = Expression.Parameter(GetElementType(source), expressionBuilder.Method.GetParameters()[0].Name);
    DynamicExpressionBuilder dynamicExpression = expressionBuilder(new DynamicExpressionBuilder(parameterExpression));

    Expression body = dynamicExpression.Expression;
    return Expression.Lambda<Func<T, bool>>(body, parameterExpression);
}

这允许“或”在这样的循环中加入通用谓词...

predicateToAdd = query.DynamicWhereForPredicateBuilder(z => z[columnName].Contains(dataTablesRequest.sSearch)); //contains or

完整的文章是 此处,完整演示位于 MvcCms.CodePlex

I have made something for this. Using David Fowlers DynamicLinq project in combination with a PredicateBuilder you can build predicates from a Generic IQueryable and build them. Special thanks to some StackOverflow answer that gave me this line to convert from

Func<dynamic, dynamic>

to:

Expression<Func<T, bool>>

Implmentation..

private static Expression<Func<T, bool>> GetFuncTbool<T>(IQueryable source, Func<dynamic, dynamic> expressionBuilder)
{
    ParameterExpression parameterExpression = Expression.Parameter(GetElementType(source), expressionBuilder.Method.GetParameters()[0].Name);
    DynamicExpressionBuilder dynamicExpression = expressionBuilder(new DynamicExpressionBuilder(parameterExpression));

    Expression body = dynamicExpression.Expression;
    return Expression.Lambda<Func<T, bool>>(body, parameterExpression);
}

This allows "or" joining generic Predicates in a loop like this...

predicateToAdd = query.DynamicWhereForPredicateBuilder(z => z[columnName].Contains(dataTablesRequest.sSearch)); //contains or

The full write up is here and the full demonstration is in the source at MvcCms.CodePlex

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