我有一個WebForm
與4個組合框,它允許用戶定義不同的搜索條件。如果用戶在組合框中沒有選擇任何東西,這意味着用戶想要選擇這個標準中的所有數據(換句話說,省略過濾器)。如何將多標準搜索應用於LINQ?
這個想法很簡單,但是當我在Linq中實現它時,它痛苦。我不知道如何可選地將條件添加到查詢中。這裏是我的「概念」查詢,當然這是不起作用的:
var context = new Entities();
var complaints = from c in context.Complaints
join s in context.Statuses on c.Status equals s.Id
join service in context.SERVICES on c.ServiceId equals service.Id
join u in context.Users on c.CreatedBy equals u.UserId
from technician in context.Users.Where(technician => technician.UserId == c.AssignedTo).DefaultIfEmpty()
where c.ResellerId == CurrentUser.ResellerID
&& c.CreatedBy == (decimal)ASPxComboBoxSupporter.Value //if Supporter is selected and so on
&& c.AssignedTo == (decimal)ASPxComboBoxTechnician.Value
&& c.ServiceId == (decimal)ddlService.Value
&& c.Status == (decimal)ddlStatus.Value
select new
{
c.Id,
c.Status,
s.Name,
c.ServiceId,
Service = service.Name,
c.Title,
c.Customer,
c.Description,
c.CreatedDate,
c.CreatedBy,
Author = u.Username,
c.AssignedBy,
c.AssignedTo,
Technician = technician.Username,
c.AssignedDate
};
我該怎麼做?
工程很好,但有一個小缺陷:decimal.TryParse不能把第一個參數作爲對象,而是字符串。我改爲ASPxComboBoxSupporter.Value作爲字符串,它的工作:) – Vimvq1987 2011-03-11 06:58:44