Thilina De Silva
Thilina De Silva

Reputation: 391

How to join two tables and a get list to view using viewmodel?

i have two tables called "Events" and "EventUser",

 public partial class Event
{
    public Event()
    {
        this.EventUsers = new HashSet<EventUser>();
        this.Objects = new HashSet<Object>();
    }

    public int Id { get; set; }
    public Nullable<int> CreatedBy { get; set; }
    public Nullable<System.DateTime> StartTime { get; set; }
    public Nullable<bool> IsShared { get; set; }
    public Nullable<double> Budget { get; set; }

    public virtual User User { get; set; }
    public virtual ICollection<EventUser> EventUsers { get; set; }
    public virtual ICollection<Object> Objects { get; set; }
}

and

 public partial class EventUser
{
    public Nullable<int> EventId { get; set; }
    public Nullable<int> UserId { get; set; }
    public bool IsAccepted { get; set; }
    public int EUPK { get; set; }

    public virtual Event Event { get; set; }
    public virtual User User { get; set; }
}

when the parameter of UserId is passed to the action method i'm planing to display all the events that have invited to that UserId and the all other users who has invited to each of that event...

For a example if the parameter userId is 2; i want to display all the EventIds of userId 2 & for each EventId all the UserIds and other details..

I tried this & i've got some information about the events but not the list of users who have also invited for each event..

here is my action method;

public ActionResult Index(int UId)

    {

        ViewBag.NotCount = 1;

        var result1 = from eu in db.EventUsers where eu.UserId == UId select eu;



        var result2 = from eu1 in result1
                      join e in db.Events on eu1.EventId equals e.Id
                      select new MessageViewModel {

                      EventID1 = e.Id,
                      IsAccepted= eu1.IsAccepted,
                      CreatedBy = e.CreatedBy,
                      Budget = e.Budget,


                      };




        return View(result2);
    }

and my view is;

@{var counter = 1;}

@if (!Model.Any())
{
    <div  style="margin-left:550px;margin-top:200px">
        <h2>"There are no notifications for you"</h2>

    </div>

}

else { 

foreach (var item in Model)
{


        <div class="panel panel-info" style="width:700px;margin-left:400px">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
        <div class=" panel-heading">
            <h3 class="panel-title">Notification @counter</h3>
        </div>
        <div class="panel-body"><p>Event @item.EventID1 is created by @item.CreatedBy .Budget of the event is @item.Budget <p>

@if (item.IsAccepted)
{
<p>You have accept the invitation!!!</p>
}

@if (!item.IsAccepted)
{
    <p>You haven't accept the invitation yet!!!!</p>
}

            @{counter++;}

      </div>
    </div>


}

and my viewmodel is;

  public class MessageViewModel
{
    public int EventID1 { get; set; }
    public bool IsAccepted { get; set; }
    public int? CreatedBy { get; set; }
    public DateTime StartTime { get; set; }

    public bool IsShared { get; set; }
    public int ObjectID { get; set; }
    public double? Budget { get; set; }

    public IEnumerable<EventUser> Euser { get; set; }
   // don't know if this is the
   // right way to get list of userids
}

how can i overcome this problem??are there any approaches other than this if my approach is wrong??

Upvotes: 0

Views: 74

Answers (1)

Bob Tway
Bob Tway

Reputation: 9613

If you're not using lazy loading, you have to specify relationships you want manually using the Include command. It's called "join" in the sql-like query syntax you're using.

You've included a join between Event and EventUser, but another join is required between EventUser and User, because User is a different entity.

I can't help you with the code, because we can't tell what the primary key on your User object is. But look at the join syntax you've already got in place, and add another join between EventUser and User, following the same format.

Upvotes: 1

Related Questions