I show businesses and people in one result and would like to order them together.
pseudo code:
if business name is not null
order by business name
else
order by first name then last name
I built a LINQ query that connects to multiple tables (and works fine) and looks something like this.
var query = from x in ...
join business in dbContext.Businesses on ...
from businesses in bus.DefaultIfEmpty()
join person in dbContext.People on ...
from people in peo.DefaultIfEmpty()
select new Party
{
person.FirstName,
person.LastName,
business.Name,
};
I can’t understand how to write a LINQ expression that orders ONLY the company name if the record is a business, another person’s first and last name. The closest I can get is the following, which always includes ordering by the person’s name (even for businesses).
var result =
whereQuery
.OrderBy(x => x.BusinessName != null ? x.BusinessName : x.PersonFirstName)
.ThenBy(x => x.PersonLastName);
What converts to SQL:
ORDER BY CASE
WHEN [business].[Name] IS NOT NULL
THEN [business].[Name]
ELSE [person].[FirstName]
END, [person].[LastName]
I would like to:
ORDER BY CASE
WHEN [business].[Name] IS NOT NULL
THEN [business].[Name]
ELSE [person].[FirstName], [person].[LastName]
END
This is what I would like to write (obviously, this is the wrong syntax for the false part):
var result =
whereQuery
.OrderBy(x => x.BusinessName != null ? x.BusinessName : x.PersonFirstName, x.PersonLastName);
( , , ). SQL -, .