gpt4 book ai didi

asp.net-mvc - 如何在 Entity Framework 中重用投影?

转载 作者:行者123 更新时间:2023-12-04 17:37:59 26 4
gpt4 key购买 nike

我有一个 ASP.NET MVC 应用程序,它使用 Entity Framework 来获取数据。

在将实体传递给 View 之前,我需要将实体转换为模型。投影可能非常复杂,但为了简单起见:

public static IQueryable<UserModel> ToModel(this IQueryable<User> users)
{
return from user in users
select new UserModel
{
Name = user.Name,
Email = user.Email,
};
}

这可以在这样的 Controller 中使用:
return View(Repository.Users.ToModel().ToList());

非常好。但是如果我想在另一个投影中使用这个投影怎么办?例子:
public static IQueryable<BlogPostModel> ToModel(this IQueryable<BlogPost> blogs)
{
return from blogs in blogs
select new BlogPostModel
{
Title = blog.Title,
Authors = blog.Authors.AsQueryable().ToModel(), // (entities are POCOs)
// This does not work, because EF does not understand method ToModel().
};
}

(假设博客可以有不止一个作者,并且它是用户类型)。

我可以以某种方式将投影分开并在另一个投影中重用它们吗?

最佳答案

这里有一些实际工作(在一个简单的测试应用程序中)只选择请求的字段:

namespace Entities
{
public class BlogPost
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual DateTime Created { get; set; }
public virtual ICollection<User> Authors { get; set; }
}

public class User
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Email { get; set; }
public virtual byte[] Password { get; set; }
public virtual ICollection<BlogPost> BlogPosts { get; set; }
}
}

namespace Models
{
public class BlogPostModel
{
public string Title { get; set; }
public IEnumerable<UserModel> Authors { get; set; }
}

public class UserModel
{
public string Name { get; set; }
public string Email { get; set; }
}

public static class BlogPostModelExtensions
{
public static readonly Expression<Func<BlogPost, BlogPostModel>> ToModelConverterExpression =
p =>
new BlogPostModel
{
Title = p.Title,
Authors = p.Authors.AsQueryable().Select(UserModelExtensions.ToModelConverterExpression),
};

public static readonly Func<BlogPost, BlogPostModel> ToModelConverterFunction = ToModelConverterExpression.Compile();

public static IQueryable<BlogPostModel> ToModel(this IQueryable<BlogPost> blogPosts)
{
return blogPosts.Select(ToModelConverterExpression);
}

public static IEnumerable<BlogPostModel> ToModel(this IEnumerable<BlogPost> blogPosts)
{
return blogPosts.Select(ToModelConverterFunction);
}
}

public static class UserModelExtensions
{
public static readonly Expression<Func<User, UserModel>> ToModelConverterExpression =
u =>
new UserModel
{
Name = u.Name,
Email = u.Email,
};

public static readonly Func<User, UserModel> ToModelConverterFunction = ToModelConverterExpression.Compile();

public static IQueryable<UserModel> ToModel(this IQueryable<User> users)
{
return users.Select(ToModelConverterExpression);
}

public static IEnumerable<UserModel> ToModel(this IEnumerable<User> users)
{
return users.Select(ToModelConverterFunction);
}
}
}

要在不实际创建数据库的情况下对其进行测试:
var blogPostsQuery = (
from p in context.BlogPosts
where p.Title.StartsWith("a")
select p).ToModel();
Console.WriteLine(((ObjectQuery)blogPostQuery).ToTraceString());

关于asp.net-mvc - 如何在 Entity Framework 中重用投影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11978130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com