C# LINQ到实体:Lambda表达式错误

C# LINQ到实体:Lambda表达式错误,c#,linq,lambda,C#,Linq,Lambda,我有3个单选按钮Any,单个和多个。它们是我的网格视图中记录的过滤器 Any不要对查询应用任何条件 Single和Multiple分别对查询应用检查条件1或2 这段代码有一些错误,但我无法解决它或找到解决方法 public void ApplyFilter() { var subQueryGroup = from hero in HDA.Hero_Group where hero.GroupID == ((byte)cboGr

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

Any
不要对查询应用任何条件

Single
Multiple
分别对查询应用检查条件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);
    }
我在案例1中遇到的错误是:

  • “实例参数:无法从'System.Linq.IQueryable'转换为'System.Linq.ParallelQuery' "

  • 'System.Linq.IQueryable'不包含'Contains'的定义,并且最佳扩展方法重载'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery,TSource)'具有一些无效参数

  • 我在案例2中遇到的错误是:

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



    请注意,我做了一些类似于组筛选的操作,但在本例中没有出现错误。

    这对顶部部分有效,但我仍然在
    q=q.Where(o=>subQuerySpec.Contains(o.ID))这是因为Where的结果将其强制转换为不同的类型。您需要像我在上面的回答中所做的那样重构这些语句,或者将结果分配给一个新变量(再次声明为type var以自动获取返回类型)。我通过创建另一个子查询,然后将其更改为在主查询中使用的所需类型来解决这个问题。现在一切都好了。谢谢,很高兴你能用上。如果你觉得我的回答很有帮助,如果你能将其标记为已接受,那将是非常好的?谢谢
    
    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;