English 中文(简体)
动态地汇编LINQ查询,以核实字典价值
原标题:Dynamically compiling LINQ queries to verify a dictionary value

让我们相信,我们需要对实体名单提出质询,我们不知道哪些标准相当活跃,实体既有独裁者,也有内部的简单领域。 让我们成为下一个实体——地址(我只留下一个财产作简便)。

public class Address
{
    #region Public members

    /// <summary>
    /// The extra datafield values
    /// </summary>
    public IDictionary<string, string> DataFieldValues { get; set; }

    public string City { get; set; }

    #endregion
}

现在,如果我们问一下一个称为“<>City的固定领域,那么我就要执行:

   private static Expression<Func<Address, bool>> BuildLambdaForAQueryItem(string caption, string value)
        {
            ParameterExpression param = Expression.Parameter(typeof(Address), caption);
            BinaryExpression body = Expression.Equal(Expression.PropertyOrField(param, caption),
                                                     Expression.Constant(value,
                                                                         typeof(Address).GetProperty(
                                                                             caption).PropertyType));
            return Expression.Lambda<Func<Address, bool>>(body, param);
        }

现在请问一下<>Data FieldValue冷却 我也有必要写出一个类似的文件:

x=>x.DataFieldValues.ContatinsKey(key) && DataFieldValues[key]==value What I get with the method below is almost similar but still it does not apply the filter correctly:

private static Expression<Func<Address, bool>> BuildLambdaForAnExtraField(PostedQueryItem queryItem)
{
    ParameterExpression dataFields = Expression.Parameter(typeof(Address), "x");
    var dictionaryExpression = Expression.PropertyOrField(dataFields, "DataFieldValues");
    var keyExists = Expression.Call(dictionaryExpression, "ContainsKey", null, Expression.Constant(queryItem.Caption));

    Expression dictionaryAccessExpr = Expression.Property(dictionaryExpression, "Item",
                                                           Expression.Constant(queryItem.Caption));
    var valueCorresponds = Expression.Equal(dictionaryAccessExpr, Expression.Constant(queryItem.Value));

    return Expression.Lambda<Func<Address, bool>>(keyExists, dataFields).And(
      Expression.Lambda<Func<Address, bool>>(valueCorresponds, dataFields));
}
最佳回答

我认为,你希望使用Expression。 AndAlso(短路和短路),内容是关于建造表达――树木体的两种前提。

var body = Expression.AndAlso(keyExists, valueCorresponds);
return Expression.Lambda<Func<Address, bool>>(body, dataFields);

www.un.org/Depts/DGACM/index_spanish.htm (如果你想要坚持现有的技术)

我的猜测是,你的<代码>。 http://www.albahari.com/nutshell/linqkit.aspx” rel=“nofollow” 如果是的话,指出这一延伸涉及援引右侧表述作为产生结果的一部分。 如果这是你可以接受的(准则Q提供人的限制,或许是吗?),那么你可以使用<条码>Expand的延伸,该扩展也是与这个图书馆一起,以说明所援引的表述。

return Expression.Lambda<Func<Address, bool>>(keyExists, dataFields)
                 .And(Expression.Lambda<Func<Address, bool>>(valueCorresponds, dataFields))
                 .Expand();

But this is massive overkill in this case; my advice is to go with my first sample.

问题回答

暂无回答




相关问题
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. ...

热门标签