LINQ到实体:Lambda表达式错误

本文关键字:表达式 错误 Lambda 实体 LINQ | 更新日期: 2023-09-27 18:07:54

我有3个单选按钮Any, SingleMultiple。它们是网格视图中记录的过滤器。

Any不应用任何查询条件。

SingleMultiple分别对查询应用检查条件1或2。

这段代码有一些错误,但我无法解决它或找到一个工作。

public void ApplyFilter()
    {
        var subQueryGroup = from hero in HDA.Hero_Group
                       where hero.GroupID == ((byte)cboGroup.SelectedValue)
                       select hero.HeroID;
        #region Case1
        /*
        var subQuerySpec = from h in HDA.Hero_Speciality
                           select new { h.HeroID, h.SpecID };
        if (rbtnMulti.Checked)
            subQuerySpec = subQuerySpec.Where(h => h.SpecID == 1);
        if (rbtnSingle.Checked)
            subQuerySpec = subQuerySpec.Where(h => h.SpecID == 2);
        */
        #endregion
        #region Case2
        var subQuerySpec = from h in HDA.Hero_Speciality
                           select h.HeroID;
        if (rbtnMulti.Checked)
            subQuerySpec = subQuerySpec.Where(h => h.SpecID == 1); // error @ h.SpecID
        if (rbtnSingle.Checked)
            subQuerySpec = subQuerySpec.Where(h => h.SpecID == 2); // error @ h.SpecID
        #endregion
        var q = from o in HDA.HeroInfoes
                orderby o.HeroRarity
                select new Hero
                {
                    ID = o.ID,
                    Name = o.HeroName,
                    RarityID = o.HeroRarity,
                    Rarity = o.Rarity.RarityName,
                    Speed = o.Initiative,
                    Attack = o.Attack,
                    Target = o.Attack2.Description
                };
        if (cboRarity.SelectedIndex != 0 && cboRarity.SelectedIndex != -1)
            q = q.Where(o => o.RarityID == cboRarity.SelectedIndex);
        if (cboGroup.SelectedIndex != 0 && cboGroup.SelectedIndex != -1)
            q = q.Where(o => subQueryGroup.Contains(o.ID));
        if (cboSpeed.SelectedIndex != 0 && cboSpeed.SelectedIndex != -1)
            q = q.Where(o => o.Speed == cboSpeed.SelectedIndex);
        q = q.Where(o => subQuerySpec.Contains(o.ID)); // Case1(error) , Case2(OK)
        ViewResults(q);
    }

我在Case1得到的错误是:

  1. "实例参数:无法从'System.Linq. linq . '转换。改为System.Linq.ParallelQuery"

  2. "来。IQueryable'不包含'Contains'的定义和最好的扩展方法重载'System.Linq. parallelenumerable .Contains(System.Linq. linq . parallelenumerable .Contains)。

我在Case2得到的错误是:

"'short'不包含'SpecID'的定义,并且没有扩展方法'SpecID'接受类型'short'的第一个参数可以找到(您是否缺少using指令或程序集引用?)"


注意,我做了一些类似于组过滤的事情,但在这种情况下,我没有得到错误

LINQ到实体:Lambda表达式错误

var subQuerySpec = from h in HDA.Hero_Speciality
                           select new { h.HeroID, h.SpecID };
        if (rbtnMulti.Checked)
            subQuerySpec = from h in subQuerySpec
                           where h.SpecID == 1
                           select h;
        if (rbtnSingle.Checked)
            subQuerySpec = from h in subQuerySpec
                           where h.SpecID == 2
                           select h;