LINQ - if condition

Posted by ile on Stack Overflow See other posts from Stack Overflow or by ile
Published on 2010-06-16T08:44:15Z Indexed on 2010/06/16 9:02 UTC
Read the original article Hit count: 171

Filed under:
|

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);

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about if-condition