在代码中,被注释的部分是我需要解决的...是否有一种方法可以用LINQ编写这样的查询?我需要这个是因为我需要基于状态进行排序。
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);
deals.Count()
而不是Deal
之外,没有其他要求。如果有超过两种情况怎么办?是否有可能做到这一点?谢谢! - ilija veselica