2016-07-27 23 views
2

努力獲得此權利。 因此,我有一個模型視圖,其中有Ienumerable BusinessUnits綁定視圖的ListBox多選,我有Ienumerable selectedBusinessUnit來保存選定的值。 一旦選擇了多個值並回發給控制器,我正努力使用linq查詢來檢查數據庫中的值與IEnumberable selectedBusinessUnit的對應關係。更像是一個SQL「IN」查詢。IN使用LINQ查詢IEnumerable項目列表

代碼如下

public class ProjectsSearchViewModel 
    { 
     public IEnumerable<string> selectedBusinessUnit { get; set; } //Selected Revenue Organization (Business Unit) 
     [Display(Name = "Business Unit")] 
     public IEnumerable<SelectListItem> BusinessUnits { get; set; } //populate list of business units from database for the listbox 
    } 

//我查看

@Html.LabelFor(m => m.BusinessUnits, new { @class = "col-md-4 control-label" }) 
@Html.ListBoxFor(m => m.selectedBusinessUnit, Model.BusinessUnits, new { @class = "form-control", type = "dropdownlist", size = 5 }) 

//控制器

if (projectsSearchViewModel.selectedBusinessUnit != null) 
       {     
        result = result.Where(x => x.Project.Revenue_Organization.Contains(projectsSearchViewModel.selectedBusinessUnit));} 

它說不能轉換IEnumerable的字符串

有沒有人能夠做到索姆像這樣成功了。如果真是這樣的話,他們會熱衷於知道如何。

更多代碼

var results = (from project in db.Projects 
     join emp in db.Employees 
     on project.Project_Manager equals emp.Employee_ID 
     select new ProjectsList 
     { 
      Project = project, 
      Employee = emp 
     }); 

Revenue_organization是其中一列中的任何一個被懷疑的情況下,該項目表(工程對象)(如果你喜歡的屬性)。 所以最終的結果我找得到的項目列表,其中revenue_organization IN選擇businessunits

+0

'result = result.Where(x => x.Project.Revenue_Organization.Contains(projectsSearchViewModel.selectedBusinessUnit));'。 'projectsSearchViewModel.selectedBusinessUnit'是IEnumerable 你不能使用包含。將語句更改爲'result = result.Where(x => x.Project.Revenue_Organization.Contains(projectsSearchViewModel.selectedBusinessUnit.FirstOrDefault()));' – din

+3

'Revenue_Organization'屬性的類型是什麼? – Shyju

+0

@ shyu物業是字符串 – vish

回答

2

假設Project.Revenue_Organization的名單,你需要扭轉的語句的字符串。集合應該包含字符串,但正如你寫它檢查字符串是否包含集合(這沒有意義)。

result = result.Where(x => projectsSearchViewModel.selectedBusinessUnit.Contains(x.Project.Revenue_Organization));} 
+1

輝煌的工作像一個魅力。非常感謝@Igor – vish

1

您對錯誤的對象使用Contains。這將是周圍的其他方法:

​​

我有這樣的假設:

projectsSearchViewModel.selectedBusinessUnit是一個IEnumerable。 x.Project.Revenue_Organization是一個字符串值(您希望在「IN」查詢中的db列中)。

+0

許多感謝@Cetin ...它的工作 – vish