English 中文(简体)
动态Linq和具有未知数的替换值?
原标题:Dynamic Linq and substitution values with unknown number?

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

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

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

有人知道这方面的办法吗?我为Where语句创建了一个连接字符串,这样我就可以添加“@1”或其他内容,但我无法向Where()添加参数,所以我陷入了困境

谢谢!

最佳回答

我为where语句创建了一个连接字符串,这样我就可以添加“@1”或其他什么,但我不能向where()添加参数,所以我陷入了困境。

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

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

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);
问题回答

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

以下是几个可供探索的替代方案。托马斯·佩特里切克的解决方案,如果你能遵循的话,特别有趣:

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


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

我为此做了些什么。将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公司





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签