So you have Payers, and every Payer has zero or more PayerStatuses.
I need to use selectmany, so I can get rows as per statuses
No idea what this means. You start mentioning the solution and then you start saying what you want, but the solution does not work? Maybe next time first specify what you want, then what you tried, then tell us why that does not work.
Besides that you didn't inform us about what you want, you also forgot to write your Payer and your PayerStatus classes .
Do you have a class "Payer with this statuses", like you would have when you'd use entity framework? Or do you only have two separate tables?
And if you only have two separate tables, how do you indicate to which Payer a PayerStatus belongs? Does every PayerStatus belong to exactly one Payer (one-to-many relation), or are there PayerStatusses that may belong to several Payers (many-to-many) or maybe there are PayerStatusses that belong to no Payer at all?
Let's assume that your relation is the most common: you have a one-to-many relation between Payers and PayerStatuses: every Payer has zero or more Payerstatuses; every PayerStatus belongs to exactly one Payer.
In tables this is usually solved using primary and foreign keys:
class Payer
{
public int Id {get; set;} // primary key
... // other Payer properties
}
class PayerStatus
{
public int Id {get; set;} // primary key
// every PayerStatus belongs to exactly one Payer using foreign key:
public int PayerId {get; set;}
... // other properties
}
If you've got tables like this, then to get "Every Payer with his PayerStatuses" you'd do a GroupJoin.
// your two tables: Payers and Statuses:
IEnumerable<Payer> payers = ...
IEnumerable<PayerStatus> statuses = ...
// GroupJoin these two tables:
var prayersWithTheirStatusses = payers.GroupJoin(statusses,
payer => payer.Id, // from each payer take the Id
status => status.PayerId, // from each status take the PayerId
(payer, statusses) => new // when they match, make a new object:
{
// take only the Payer properties you plan to use, for instance:
PayerId = payer.Id,
Name = payer.Name,
Reputation = payer.Reputation,
...
// from all payer's statuses, take only the properties you plan to use
Statusses = statusses.Select(status => new
{
Description = status.Description,
StatusValue = status.Value,
...
})
.ToList(),
}
}
This way you get every Payer with all his statuses, even if the Payer has no status at all.
So if you have the following Tables:
Payers
Id = 10, Name = A
Id = 11, Name = B
Id = 12, Name = C
PayerStatusses
Id = 21, PayerId = 10, ...
Id = 22, PayerId = 11, ...
Id = 23, PayerId = 10, ...
The linq statement above will give you something like
Payer 10, name A with Status list containing data from statuses 21 and 23
Payer 11. name B with Status list with one element containing data from status 22
Payer 12, name C with empty status list
Quite often, people don't want the Payer with his statuses, but one row per a Payer and one of his statuses. So they like the following result
10 - A - status 21
10 - A - status 23
11 - B - status 22
12 - C - (null)
I never found a proper use case where you'd prefer this above the GroupJoin. I think this is still in demand because people think in the limitations of SQL statements that doesn't have a join to get "Payers with their statuses", and so they take the next best thing, a Left Outer Join, as is answered several times on SO:
Continuing after the GroupJoin:
.SelectMany(payer => payer.Statusses.DefaultIfEmpty(),
(payer, status) => new
{
// payer properties:
PayerId = payer.PayerId,
PayerName = payer.Name,
Reputation = payer.Reputation,
// status properties:
StatusDescription = status.Description,
StatusValue = status.Value,
});