2016-07-27 3 views
2

この権利を得るために苦労します。 私はビューのListBox multiselectをバインドするためにIenumerable BusinessUnitsを持つモデルビューを持っており、選択した値を保持する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を変換することはできませんと言いますsomこれはうまくいくようなものです。もしそうなら、方法を知りたいと思うだろう。 (必要であればプロパティ)

もっとコード

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は、いずれかが疑問に思っている場合には(プロジェクトオブジェクト)プロジェクトのテーブルの列の一つです。私はbusinessunits

+0

'result = result.Where(x => x.Project.Revenue_Organization.Contains(projectsSearchViewModel.selectedBusinessUnit));'。 'projectsSearchViewModel.selectedBusinessUnit'はIEnumerable です。あなたはcontainsを使用できません。ステートメントを 'result = result.Where(x => x.Project.Revenue_Organization.Contains(projectsSearchViewModel.selectedBusinessUnit.FirstOrDefault())); ' – din

+3

に変更します。' Revenue_Organization'プロパティのタイプは? – Shyju

+0

@shyuプロパティは文字列です – vish

答えて

2

の選択INリストrevenue_organization 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

多くのおかげで@セタン...それは働いた – vish

関連する問題