动态表达,entityframwork包括扩展 [英] Dynamic expression, entityframwork include extended

查看:63
本文介绍了动态表达,entityframwork包括扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。

我有一个生成Entityframework的方法,包括来自对象的动态结构和构建如下所示的字符串:

Hi.
I have a method that generate Entityframework include dynamiclly from an object and build string that look like this:

"x=> x.Connectors , x=> x.BlockEntryFieldsGroup.Select(GhIm=> GhIm.BlockEntry_Field.Select(GhImcc=> GhImcc.BlockEntry_Value)) , x=> x.BlockEntryOffSet"





我希望将给定的字符串转换为params Expression> []包括现在我使用



and i would like to convert the given string to params Expression>[] includes right now i use

CSharpCodeProvider

来动态转换它。



有没有更好的方法来做到这一点而不使用

to dynamilcy convert it.

is there any better way to do this without using

CSharpCodeProvider





我的尝试:



这就是我现在所做的。



What I have tried:

this is what i do right now.

 var searchValue = DynamicIncludeExpressionString(typeof(T), "x");
    if (!searchValue.Any())
        return source;
    var func = "x=> " + string.Join(" , x=> ", searchValue);
    var builder = new StringBuilder();
    builder.Append("\r\nusing System;");
    builder.Append("\r\nusing System.Collections.Generic;");
    builder.Append("\r\nusing System.Linq;");
    builder.Append("\r\nusing System.Reflection;");
    builder.Append("\r\nusing SourceTech.Core.Interface;");
    builder.Append("\r\nusing SourceTech.Core.ObjectLibrary;");
    builder.Append("\r\nusing SourceTech.Core.Expression;");
    builder.Append("\r\nnamespace DynamicLinqInclude");
    builder.Append("\r\n{");
    builder.Append("\r\npublic sealed class Dynamic_LinqInclude");
    builder.Append("\r\n{");
    builder.Append("\r\npublic static IQueryable<" + type.FullName + "> Execute<T>(IQueryable<" + type.FullName + "> source)");
    builder.Append("\r\n{");
    builder.Append("\r\n return source.IncludeEntitys(" + func + ");");
    builder.Append("\r\n}");
    builder.Append("\r\n}");
    builder.Append("\r\n}");
    var codeProvider = new CSharpCodeProvider();
    var compilerParameters = new CompilerParameters
    {
        GenerateExecutable = false,
        GenerateInMemory = false,
        CompilerOptions = "/optimize"
    };
    compilerParameters.ReferencedAssemblies.Add("System.dll");
    compilerParameters.ReferencedAssemblies.Add(typeof(System.Linq.IQueryable).Assembly.Location);
    compilerParameters.ReferencedAssemblies.Add(typeof(System.Collections.IList).Assembly.Location);
    compilerParameters.ReferencedAssemblies.Add(typeof(System.Linq.Enumerable).Assembly.Location);
    compilerParameters.ReferencedAssemblies.Add(typeof(T).Assembly.Location);
    string sourceCode = builder.ToString();
    CompilerResults compilerResults = codeProvider.CompileAssemblyFromSource(compilerParameters, sourceCode);
    Assembly assembly = compilerResults.CompiledAssembly;
    Type types = assembly.GetType("DynamicLinqInclude.Dynamic_LinqInclude");
    MethodInfo methodInfo = types.GetMethod("Execute");
    methodInfo = methodInfo.MakeGenericMethod(type);
    if (!DynamicLinqIncludeSources.ContainsKey(key)) // it may have been added by another threads
    {
        DynamicLinqIncludeSources.Add(key, methodInfo); // Save it in a temp instead of memory so we could choose to Clear it later.
        DynamicLinqIncludeSourceLength++;
    }
}

return (IQueryable<T>)DynamicLinqIncludeSources[key].Invoke(null, new object[] { source });

推荐答案

包装IQueryable< t>可能更容易,更清晰。由DbContext.Set< t>()提供,而不是生成一个由编译器解析的字符串。实际上,您可以通过使用表达式树而不需要Codedom。
It may be easier and cleaner to wrap IQueryable<t> provided by DbContext.Set<t>() instead of produce a string to parse by compiler. Indeed you can take advantage by using expression tree and not required Codedom.


这篇关于动态表达,entityframwork包括扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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