[英]How to include a entity specific property in Entity Framework?
I am working with Entity Framework. 我正在使用实体框架。 I don't have enough experience to solve any kinds of problem. 我没有足够的经验来解决任何问题。 The problem I am facing now is this: I have two classes as shown here: 我现在面临的问题是:我有两个类,如下所示:
public class AspNetUser: NormalUser
{
[Key]
public string UserId { get; set; }
public string PIN { get; set; }
public string FullName { get { return this.LastName + " , " + this.FirstName; } }
}
public class OfferReview
{
[Key]
public string OfferReviewId { get; set; }
public string UserId { get; set; }
public string Review { get; set; }
public virtual AspNetUser User { get; set; }
}
I need to bind all OfferReview
property with AspNetUser.FullName
property ==> I tried like this: 我需要将所有OfferReview
属性与AspNetUser.FullName
属性绑定==>我尝试这样:
return context.OfferReviews
.Where(It => It.OfferId == offerId)
.Include(it => it.User.FullName)
.ToList();
Here offerId
is a function parameter. 这里的offerId
是一个函数参数。 I'm unable to show the full function.... 我无法显示全部功能。
I can easily get the above requirement with the help of linq join. 我可以在linq join的帮助下轻松获得上述要求。 But I want something above like lambda expression. 但是我想要像lambda表达式这样的东西。
Is it possible? 可能吗? Or if possible how also if not possible then is there any other way? 或者,如果可能的话,如果不可能的话,还有其他方法吗? Please help 请帮忙
Your query should be: 您的查询应为:
return context.OfferReviews
.Where(It => It.OfferId == offerId)
.Include(it => it.User)
.ToList();
This return a list of OfferReview
entities, each having a User
object in it. 这将返回OfferReview
实体的列表,每个实体中都有一个User
对象。
Now in your view you can bind OfferReview.User.FullName
. 现在,您可以在视图中绑定OfferReview.User.FullName
。
You Can Do It By Technique ViewModel 你可以通过技术来做到
This ViewModel : 这个ViewModel:
public class ViewModel
{
public string OfferReviewId { get; set; }
public string UserId { get; set; }
public string Review { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string FullName { get { return this.LastName + " , " + this.FirstName; } }
}
this query: 此查询:
var query = context.OfferReview
.Where(It => It.OfferReviewId == "1")
.Select(p => new ViewModel
{
OfferReviewId = p.OfferReviewId,
Review = p.Review,
UserId = p.UserId,
FirstName = p.User.FirstName,
LastName = p.User.LastName
}).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.