0
我對使用T-SQL/SQL Server相當陌生,而且我不確定如何使用Linq獲得期望的結果。如何對結果進行分組並刪除重複項
從下面的代碼,我得到如下結果:
from com in Complaints
join pro in Projects on com.ProjectId equals pro.ProjectId
join cont in Contacts on com.SubContractorContactId equals cont.ContactId
join cont2 in Contacts on com.OccupantContactId equals cont2.ContactId
where com.EmailStatusId == null
select new
{
pro.ProjectName,
com.ComplaintId,
com.ComplaintName,
SubContractorName = cont.FirstName,
SubContractorEmail = cont.Email,
com.DueDate,
OccupantName = cont2.FirstName,
OccupantPhone = cont2.Phone,
OccupantEmail = cont2.Email
}
我想結果與其中列出的每個投訴返回一個不同的項目。此外,我希望在投訴中對每個聯繫人進行分組,同時刪除重複內容。因此,對於每個項目,我希望得到的結果是單行的,然後是項目內的每個投訴,並在每個投訴聯繫人內。
爲了幫助您理解我創建了這個大綱。
Projects
- Lake Road Construction
- Complaints
- Foundation Cracks: ComplaintId 1, DueDate
- Contacts
- SubContractor
- Carlo
- [email protected]
- Occupant
- William
- [email protected]
- 616-846-0467
- Drywall: ComplaintId 2, DueDate
- Contacts
- SubContractor
* No SubContractor needed since the previous complaint has the same SubContractor.
- Occupant
- Keesha
- [email protected]
- 661-205-2018
- Jennifer Lane Renovation
- Complaints
- Light Fixture: ComplaintId 3, DueDate
- Contacts
- SubContractor
- Ardith
- [email protected]
- Occupant
- Olivia
- [email protected]
- 210-530-5281
- Plumbing: ComplaintId 4, DueDate
- Contacts
* Contacts have already been collected from the other complaint.
從我讀過的內容看,我認爲我需要使用「group by new {}」或使用UNION。不過,我不確定我需要的方向。任何幫助將不勝感激。
你是一個生命的救星!我從來沒有想過這樣做。看起來我需要刷新我的Linq/Lambda。 –