I have three objects (splistitemcollection) that are combined together, but the problem is that there is a one-to-many relationship between the contract object and the client object. I need to capture only the first client object for each contract object during the connection.
This is what i get
(Contract) (Customer)
12345 John Smith
12345 Jane Smith
67890 howard jones
67890 Mary Jones
That's what I want 12345 (only one of the clients, Jane or John)
Here is the code that is currently in use.
var joinedResults = from SPListItem contracts in _contractList
join SPListItem customers in _customerList
on contracts["ContractNumber"] equals customers["ContractNumber"]
join SPListItem loans in _loanList
on contracts["ContractNumber"] equals loans["Contract_x0020_Number"]
into l from loans in l.DefaultIfEmpty()
select new MergedData(contracts, customers, loans);
In SQL, I would define the select top clause in the subquery defined in my connection, I just can't wrap my head around the syntax for my new linq.
Final result
var joinedResults = from SPListItem contracts in _contractList
join SPListItem customers in
(from SPListItem customers in _customerList
group customers by customers["ContractNumber"] into groupedCustomers
select groupedCustomers.FirstOrDefault()
) on contracts["ContractNumber"] equals customers["ContractNumber"]
join SPListItem loans in _loanList
on contracts["ContractNumber"] equals loans["Contract_x0020_Number"] into l
from loans in l.DefaultIfEmpty()
select new MergedData(contracts, customers, loans);