I have three models (Applicant, Meeting, City) and I joined three of them. I want to get Count by grouping MeetingId in Applicant model. Here are my models and method I use for populating Dropdownlist in Controller. So, like the "Expired" property in the Controller, how can I obtain the count for the MeetingId by grouping on "TotalMeetingById" property?
Applicant Model:
public class Applicant
{
public int ApplicantID { get; set; }
public DateTime? SubmitDate { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int MeetingId { get; set; }
}
Meeting Model:
public class Meeting
{
public int MeetingID { get; set; }
public string MeetingName { get; set; }
public DateTime MeetingStartDate { get; set; }
public DateTime? MeetingEndDate { get; set; }
public int? TotalParticipant { get; set; }
public int? MeetingCityId { get; set; }
public int? ParticipantCityAId { get; set; }
public int? ParticipantCityBId { get; set; }
}
City Model:
public class City
{
public int CityID { get; set; }
public string CityName { get; set; }
}
Controller:
private void PopulateDropDownList()
{
var meetingsQuery = repository.Meetings
.GroupJoin(repository.Cities, m => m.MeetingCityId, c => c.CityID, (m, c) => new { m, cA = c.DefaultIfEmpty() })
.SelectMany(z => z.cA.Select(cA => new { m = z.m, cA }))
.GroupJoin(repository.Cities, m => m.m.ParticipantCityAId, c => c.CityID, (m, c) => new { m.m, m.cA, cB = c.DefaultIfEmpty() })
.SelectMany(w => w.cB.Select(cB => new { m = w.m, cA = w.cA, cB }))
.GroupJoin(repository.Cities, m => m.m.ParticipantCityBId, c => c.CityID, (m, c) => new { m.m, m.cA, m.cB, cC = c.DefaultIfEmpty() })
.SelectMany(t => t.cC.Select(cC => new { m = t.m, cA = t.cA, cB = t.cB, cC }))
.Select(
m =>
new
{
CityID = m.cA.CityID,
CityName = m.cA.CityName,
MeetingDate = m.m.MeetingStartDate,
MeetingName = m.m.MeetingName,
NameofMeetingCityIdA = m.cA != null ? m.cA.CityName : null,
NameofMeetingCityIdB = m.cB != null ? m.cB.CityName : null,
NameofMeetingCityIdC = m.cC != null ? m.cC.CityName : null
})
.OrderBy(x => x.CityID)
.AsEnumerable()
.Select(
i => new
{
Value = i.CityID.ToString(),
DisplayValue = string.Format("{0} ({1:dd MMMM yyyy})", i.NameofMeetingCityIdA, i.MeetingDate),
Expired = i.MeetingDate < DateTime.UtcNow,
TotalMeetingById= ??? >>> I cannot get the total count for the related MeetingId at here
}
).ToList();
var selectItems = new List<MyHelpers.MySelectItem>(meetingsQuery.Count);
foreach (var record in meetingsQuery)
{
var item = new MyHelpers.MySelectItem
{
Text = record.DisplayValue,
Value = record.Value
};
if (record.Expired)
{
item.Class = "disabled";
item.Disabled = "disabled";
}
selectItems.Add(item);
}
ViewBag.MeetingData = selectItems;
}
Here are sample data for models:
Applicant:
ApplicantID : 100
SubmitDate : 01/11/2013
Name : Christof
Surname : Jahnsen
MeetingId : 1
Meeting:
MeetingID : 1
MeetingName : City Information Days
MeetingStartDate : 01/01/2014
MeetingEndDate : 02/01/2014
TotalParticipant : 2 (for example)
MeetingCityId : 500
ParticipantCityAId : 501
ParticipantCityBId : 502
City:
CityID : 500 / 501 / 502
CityName : London / Paris / NY
Update -----------------------------------------------------------------------
Razor:
@Html.LabelFor(m => m.Applicant.MeetingId)
@Html.MyDropdownListFor(m => m.Applicant.MeetingId, ViewBag.MeetingData as List<MyHelpers.MySelectItem>, "---- Select ----",
new { name = "meetingId", id = "meetingId" })
@Html.ValidationMessageFor(m => m.Applicant.MeetingId, null, new { @class = "ValidationErrors" })
Controller:
public ViewResult Add()
{
PopulateDropDownList();
ApplicantViewModel model = new ApplicantViewModel
{
Applicant = new Applicant(),
Applicants = repository.Applicants,
Lookups = repository.Lookups,
Cities = repository.Cities
.ToList()
};
return View(model);
}
[HttpPost]
public ActionResult Add(ApplicantViewModel model)
{
ApplicantViewModel viewModel;
if (ModelState.IsValid)
{
model.Applicant.SubmitDate = DateTime.Now;
repository.SaveApplicant(model.Applicant);
PopulateDropDownList(model.Applicant);
return View("Completed", ViewBag.ApplicantId = model.Applicant.ApplicantID);
}
else
{
// there is something wrong with the data values
PopulateDropDownList();
TempData["message"] = "Please try again.";
viewModel = new ApplicantViewModel
{
Applicant = new Applicant(),
Applicants = repository.Applicants,
Lookups = repository.Lookups,
Cities = repository.Cities
.ToList()
};
return View(viewModel);
}
}
groupbyextension Queryable.GroupBy or Enumerable.GroupBy