Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
I have an listing that have multiple listingmessages. I want to show which listingmessages the current user have written on a specific listing.

In sql I will have written this :
SQL
SELECT * FROM Listings INNER JOIN ListingMessages on Listings.ListingId = ListingMessages.ListingId WHERE ListingMessages.UserId = userID


But how do I do this in the entitiy framework.

Model for Listing :

public class Listing
   {

       public int ListingId { get; set; }

       [Required(ErrorMessage = "Angiv venligst en titel og udgave")]
       [Display(Name = "Titel & udgave")]
       public string TitleEdition { get; set; }

       public virtual ICollection<ListingMessage> listMessage { get; set; }

   }

Model for ListingMessages :

public class ListingMessage
 {
     public int ListingMessageId { get; set; }

     [Display(Name = "Send besked")]
     [Required(ErrorMessage = "Angiv venligst en besked")]
     public string Message { get; set; }

     public string UserId { get; set; }

     public virtual Listing Listing { get; set; }
     public int ListingId { get; set; }


 }


What I have tried in the controller :

What I have tried:

public ActionResult ProfileMessageListing()
        {
            var user = User.Identity.GetUserId();

            //var list = (from l in db.Listings
            //            join lm in db.ListingMessages on l.ListingId equals lm.ListingId
            //            where lm.UserId == user
            //            select l);

            //var list = db.Listings.Include(x => x.listMessage).Where(d => d.listMessage.Count > 0).ToList();

            var listmsg = db.ListingMessages.Where(d => d.UserId == user).ToList();


            var list = db.Listings.Where(s => s.ListingId == listmsg.);
            

            return View(list);
        }


Hope someone could give me a hint, I am really stucked!!
Posted
Updated 20-Apr-20 13:33pm

Use "Include" to access related tables

Entity Framework Loading Related Entities[^]
 
Share this answer
 
I found a solution that works for me. I have a .Net core MVC app using identity. I have a method in my controller where I get the Id of the user that's logged in.
C#
[HttpGet]
public IActionResult UserIndex()
{
var userId = _userManager.GetUserId(HttpContext.User); //gets the id of the logged user
List<Assign> listdata = _context.Assign.Where(u => u.Id == userId).ToList(); //create list of users
//where the Id in Assign table/model is the same as the id of the logged user
return View(listdata);
}


View:
C#
@model IEnumerable<TaskManager2.Models.Assign>

@{
    ViewData["Title"] = "UserIndex";
}

<h1>UserIndex</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.TaskId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.TaskId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.AssignId">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.AssignId">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.AssignId">Delete</a>
            </td>
        </tr>
}
    </tbody>
</table>

Hope it helps sb!
 
Share this answer
 
Comments
Richard Deeming 21-Apr-20 9:30am    
The question was about retrieving related data. Your solution contains nothing relevant to the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900