【问题标题】:How to use Expression in Expression for single element如何在单个元素的表达式中使用表达式
【发布时间】:2021-04-05 09:26:24
【问题描述】:

如何在另一个表达式中使用一个表达式。对于一组我可以使用blog.Posts.Select(postMapper.ProjectPost)。但是如何将它用于单个对象?我不想调用编译,我需要在 EF sql 翻译器中使用它。我尝试了一些技巧,例如new List<Blog>{post.Blog}.Select(blogMapper.ProjectBlog).First(),但它不起作用。

    public class BloggingContext : DbContext
    {
        private readonly ILoggerFactory loggerFactory;

        public BloggingContext(ILoggerFactory loggerFactory)
        {
            this.loggerFactory = loggerFactory;
        }

        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
        
        protected override void OnConfiguring(DbContextOptionsBuilder options)
            => options.UseLoggerFactory(loggerFactory).UseSqlServer(@"Server=(LocalDB)\MSSqlLocalDb;Database=EFExpressionMapper;Trusted_Connection=True");
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; } = new List<Post>();
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }

    class Program
    {
        static async Task Main(string[] args)
        {
            IServiceCollection serviceCollection = new ServiceCollection();
            serviceCollection.AddLogging(builder => builder
                .AddConsole()
                .AddFilter(level => level >= LogLevel.Information)
            );
            var loggerFactory = serviceCollection.BuildServiceProvider().GetService<ILoggerFactory>();

            await using var dbContext = new BloggingContext(loggerFactory);
            
            dbContext.Add(new Blog
            {
                Url = "http://blogs.msdn.com/sample-blog",
                Posts =
                {
                    new Post {Title = "Post 1", Content = "Post 1 content"},
                    new Post {Title = "Post 2", Content = "Post 2 content"},
                    new Post {Title = "Post 3", Content = "Post 3 content"},
                }
            });
            await dbContext.SaveChangesAsync();

            var postMapper = new PostMapper(new BlogMapper());

            var posts = await dbContext.Posts.Select(postMapper.ProjectPost).ToArrayAsync();

            foreach (var post in posts)
            {
                Console.WriteLine($"{post.Title} {post.Blog.Url}");
            }
        }
    }

    public class PostMapper
    {
        public Expression<Func<Post, PostDto>> ProjectPost { get; }

        public PostMapper(BlogMapper blogMapper)
        {
//TODO USE blogMapper.ProjectBlogList WITHOUT COMPILE
            ProjectPost = post => new PostDto(post.PostId, post.Title, post.Content, blogMapper.ProjectBlogList.Compile()(post.Blog));
        }
    }

    public class BlogMapper
    {
        public Expression<Func<Blog, BlogListDto>> ProjectBlogList { get; } = blog => new BlogListDto(blog.BlogId, blog.Url);
    }

    public class BlogListDto
    {
        public int BlogId { get; }
        public string Url { get; }
        
        public BlogListDto(int blogId, string url)
        {
            BlogId = blogId;
            Url = url;
        }
    }

    public class PostDto
    {
        public int PostId { get; }
        public string Title { get; }
        public string Content { get; }
        
        public BlogListDto Blog { get; }

        public PostDto(int postId, string title, string content, BlogListDto blog)
        {
            PostId = postId;
            Title = title;
            Content = content;
            Blog = blog;
        }
    }

查看 PostMapper 构造函数。我在那里使用了编译方法。但是对EF不好

【问题讨论】:

  • 在这里查看我的答案:stackoverflow.com/a/66386142/10646316
  • @SvyatoslavDanyliv 我查过了。但我不知道如何在我的场景中使用它。我需要 blogMapper.ProjectBlogList 在 PostMapper 中获取单个博客实例
  • 好吧,将为 LINQKit 准备示例。选择哪个库是您的选择。
  • LInqKIT 对我有好处。谢谢

标签: c# linq entity-framework-core


【解决方案1】:

实际上LINQKit 可能会更正您的查询并使EF 在使用Compile 时感到高兴。只需将AsExpandable() 添加到您的查询中即可。

但我建议不要为每个 DTO 创建映射类,而是将它们收集在逻辑一个中:

public static class DtoMapper
{
    [Expandable(nameof(AsDtoPost))]
    public static PostDto AsDto(this Post post)
        => throw new NotImplementedException();

    [Expandable(nameof(AsDtoBlogList))]
    public static BlogListDto AsDto(this Blog blog)
        => throw new NotImplementedException();

    static Expression<Func<Post, PostDto>> AsDtoPost()
        => post => new PostDto(post.PostId, post.Title, post.Content, post.Blog.AsDto()));

    static Expression<Func<Blog, BlogListDto>> AsDtoBlogList()
        => blog => new BlogListDto(blog.BlogId, blog.Url);
}

所以你的样本可以重写

var posts = await dbContext.Posts
    .AsExpandable()
    .Select(p => p.AsDto()).ToArrayAsync();

类似但之前已经创建了答案,其中涵盖了执行相同 lambda 表达式扩展的其他库。 https://stackoverflow.com/a/66386142/10646316 此答案侧重于 LINQKit 实现。

【讨论】:

  • 它不适用于异步版本。但这是一个有趣的解决方案Unhandled exception. System.InvalidOperationException: The source 'IQueryable' doesn't implement 'IAsyncEnumerable&lt;EFExpressionMapper.PostDto&gt;'. Only sources that implement 'IAsyncEnumerable' can be used for Entity Framework asynchronous operations.
  • 异步应该可以工作,你必须安装正确的版本:nuget.org/packages/LinqKit.Microsoft.EntityFrameworkCore
猜你喜欢
  • 1970-01-01
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 2019-01-29
相关资源
最近更新 更多