简体   繁体   English

动态 LINQ IQueryable(在运行时生成表达式)

[英]Dynamic LINQ IQueryable (Generate Expression at runtime)

I have the following query我有以下查询

query.Join(
    relationEntity,
    entity => new { Prop1 = EF.Property<object>(entity, "Id"), Prop2 = Ef.Property...},
    relation => new { Prop1 = EF.Property<object>(relation, "EntityId"), Prop2 = EF.Property... },
    (Entity, Relation) => new { Entity, Relation })

I need to generate the keys at runtime.我需要在运行时生成密钥。 How can that be achieved?如何实现? I already tried to build a Expression at runtime but it looks like the Join takes the recieved value literally so if I call a method there it gets translated to that method name instead of the result.我已经尝试在运行时构建一个表达式,但看起来 Join 从字面上获取接收到的值,所以如果我在那里调用一个方法,它会被转换为该方法名称而不是结果。

The method I'm testing with atm:我用 atm 测试的方法:

   private static object MyMethod(object entity, string name)
             => new { Id = EF.Property<object>(entity, name) };



   query.Join(
        relationEntity,
        entity => MyMethod(entity, "Id"),
        relation => MyMethod(relation, "EntityId"),
        (Entity, Relation) => new { Entity, Relation })

To Make your MyMethod an expression you have to approach it like this要使您的 MyMethod 成为表达式,您必须像这样处理它

private static Expression<Func<T, object>> MyMethod<T>(string name) => 
         (T entity) => new { Id = EF.Property<object>(entity, name) };

query.Join(
    relationEntity,
    MyMethod<entityType>("Id"),
    MyMethod<relationType>("EntityId"),
    (Entity, Relation) => new { Entity, Relation })

The Method has to return an Expression, not any computed result.该方法必须返回一个表达式,而不是任何计算结果。

But as more complex your MyMethod will be, as less likely you will be able to convert it to SQL.但是,您的 MyMethod 越复杂,您就越不可能将其转换为 SQL。

This here is more about keeping your code shorter and more readable.这更多是为了让你的代码更短、更易读。 There is nothing dynamic in there yet.那里还没有任何动态。

Something like this is not a problem, since this selection is executed before SQL-Creation.像这样的事情不是问题,因为这个选择是在 SQL-Creation 之前执行的。

private static Expression<Func<T, object>> MyMethod<T>(string name) 
{ 
    if (case 1)
         return (T entity) => new { Id = EF.Property<object>(entity, name) };
    if (case 2) 
         return (T entity) => new { Id = EF.Property<object>(entity, name), Id2 = ... something else }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM