Linq join, creates duplicate results in MVC view

  • Thread starter Thread starter Zain Nabi
  • Start date Start date
Z

Zain Nabi

Guest
I do not have duplicate records in my database however in my view. It shows duplicates. ClientId is a Guid which is passed on to each table in the join.


Please help

What I have tried:


public
ActionResult ClientInfo(
string
ClientId)
{
var
query = (
from
x
in
db.FinancialNeedsClients
join
y
in
db.FinancialSpouseDetailss on x.ClientId equals y.ClientId into ylist from y in ylist.DefaultIfEmpty()
join
z
in
db.FinancialSpouseDetailss on x.ClientId equals z.ClientId into zlist from z in zlist.DefaultIfEmpty()
join
c
in
db.FinancialNeedsChildrens on x.ClientId equals c.ClientId into clist from c in clist.DefaultIfEmpty()
join
d
in
db.FinancialNeedDependentss on x.ClientId equals d.ClientId into dlist from d in dlist.DefaultIfEmpty()
join
e
in
db.FinancialNeedContactDetailss on x.ClientId equals e.ClientId into elist from e in elist.DefaultIfEmpty()
where
x.ClientId == ClientId
select new
FinancialNeedClientInfoViewModel()
{
Id = x.Id,
ClientId = x.ClientId,
Surnname = x.Surnname,
FirstName = x.FirstName,
IDNumber = x.IDNumber,
DOB = x.DOB,
Age = (
int
?)x.Age ??
0
,
Nationality = x.Nationality,
Occupation = x.Occupation,
TaxRef = x.TaxRef,
EmailAddress = x.EmailAddress,
ContactNumber = x.ContactNumber,
MaritaStatus = x.MaritaStatus,
MaritalRegime = x.MaritalRegime,
YearOfMarriage = x.YearOfMarriage,
IfOther = x.IfOther,

SpouseId = (
int
?)y.Id ??
0
,
SpouseSurnname = y.Surnname,
SpouseFirstName = y.FirstName,
SpouseIDNumber = y.IDNumber,
SpouseDOB = y.DOB,
SpouseAge = (
int
?)y.Age ??
0
,
SpouseNationality = y.Nationality,
SpouseOccupation = y.Occupation,
SpouseTaxRef = y.TaxRef,
SpouseEmailAddress = y.EmailAddress,
SpouseContactNumber = y.ContactNumber,

ChildId = (
int
?)c.Id ??
0
,
ChildrenFIrst_Name = c.FIrst_Name,
ChildrenGender = c.Gender,
ChildrenAge = (
int
?)c.Age ??
0
,
ChildrenOccupation = c.Occupation,
ChildrenDOB = c.DOB,

DepId = (
int
?)d.Id ??
0
,
DepdendentFIrst_Name = d.FIrst_Name,
DepdendentAge = (
int
?)d.Age ??
0
,
DepdendentRelationship = d.Relationship,

ContactId = (
int
?)e.Id ??
0
,
MainMemberResedentialAddress = e.ResedentialAddress,
MainMemberPostalAddress = e.PostalAddress,
MainMemberBusinessAddress = e.BusinessAddress,
MainMemberMobileNumber = e.MobileNumber

}).Distinct().ToList();

Continue reading...
 
Back
Top