LINQ - if condition
- by ile
In code, the commented part is what I need to solve... Is there a way to write such query in LINQ? I need this because I will need sorting based on Status.
var result = (
from contact in db.Contacts
join user in db.Users on contact.CreatedByUserID equals user.UserID
join deal in db.Deals on contact.ContactID equals deal.ContactID into deals
orderby contact.ContactID descending
select new ContactListView
{
ContactID = contact.ContactID,
FirstName = contact.FirstName,
LastName = contact.LastName,
Email = contact.Email,
Deals = deals.Count(),
EstValue = deals.Sum(e => e.EstValue),
SalesAgent = user.FirstName + " " + user.LastName,
Tasks = 7,
// This is critical part
if(Deals == 0) Status = "Prospect";
else
Status = "Client";
// End of critical part...
})
.OrderBy(filterQuery.OrderBy + " " + filterQuery.OrderType)
.Where(filterQuery.Status);