0

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:

return context.OfferReviews
              .Where(It => It.OfferId == offerId)
              .Include(it => it.User.FullName)
              .ToList();

Here offerId is a function parameter. I'm unable to show the full function....

I can easily get the above requirement with the help of linq join. But I want something above like lambda expression.

Is it possible? Or if possible how also if not possible then is there any other way? Please help

2 Answers 2

1

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.

Now in your view you can bind OfferReview.User.FullName.

Sign up to request clarification or add additional context in comments.

Comments

0

You Can Do It By Technique ViewModel

This 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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.