1

次の式を使用して、ドロップダウン リストの LINQ を選択しようとしています。

    protected void PopulateInstitutionsDropDownList(Team currentTeam)
    {
        var institutions = from d in db.Institutions
                           where !(currentTeam.Institutions.Select(x => x.ID).Contains(d.ID))
                           orderby d.InstitutionName
                           select d;

        List<Institution> i = institutions.ToList();

        ViewBag.Institutions = new SelectList(i, "ID", "InstitutionName");
    }

ただし、これにより、例外が発生します: タイプ 'Refusion.Models.Institution' の定数値を作成できません。この型では、プリミティブ型または列挙型のみがサポートされています。

これが、Compare オブジェクトなしでは 2 つのオブジェクトを直接比較できないことを知っているため、新しいコレクションの ID だけを選択しようとする理由です。

なぜこれが機能しないのですか?

4

3 に答える 3

4

このようなものはどうですか?検討しましたExceptか?

IEnumerable<Institutions> institutions = db.Institutions.Except(currentTeam.Institutions);
于 2013-05-10T18:35:16.967 に答える
3

あなたのsintaxは間違っているかもしれないと思います。過去に、これを使用してユーザーの表示をブロックしました。

string[] blockedUsers = { "Jair","Jean" };


Users.Where (c => !blockedUsers.Contains (c.Name))
    //"This translates to SQL WHERE NOT ... IN"

それが役に立てば幸い。

編集1:

public class Institution
{
    public int id { get; set; }
    public string name { get; set; }
}

public class Team
{
    public int id { get; set; }
    public string name { get; set; }
    public int InstId { get; set; }
}


protected void Page_Load(object sender, EventArgs e)
{

    Team currentTeam = new Team() { id = 1, InstId = 2, name = "Team 1 in Institute 2" };

    Institution inst1 = new Institution() { id = 1, name = "Inst1" };
    Institution inst2 = new Institution() { id = 2, name = "Inst2" };
    Institution inst3 = new Institution() { id = 3, name = "Inst3" };

    List<Institution> institutions = new List<Institution>();

    institutions.Add(inst1); institutions.Add(inst2); institutions.Add(inst3);

    var allowedInst = institutions.Where(i => !(i.id == currentTeam.InstId));


    foreach (Institution inst in allowedInst)
    {
        //it should show institutes 1 and 3
        Response.Write(inst.id + " - " + inst.name + "<br/>");
    }
}
于 2013-05-10T21:31:45.690 に答える
1

問題はあなたの場所です。以下をせよ

List<int> IDs = currentTeam.Institution.Select(x=>x.ID).ToList()

次に、あなたの場所は次のようになります

where !(IDs.Any(x => x==d.ID)
于 2013-05-10T18:15:58.817 に答える