我有3个单选按钮Any
,Single
和Multiple
。它们是网格视图中记录的过滤器。
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);
}
我在Case1得到的错误是:
“实例参数:无法从'System.Linq.IQueryable'转换为'System.Linq.ParallelQuery' “
“'System.Linq.IQueryable'不包含'Contains'的定义和最佳扩展方法重载'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery,TSource)'有一些无效参数“
我在Case2遇到的错误是:
” 'short'不包含'SpecID'的定义,并且没有扩展方法'SpecID'可以找到接受类型'short'的第一个参数(你是否缺少using指令或汇编引用?)“
请注意,我做了类似于群组过滤的事情,但在这种情况下,我没有收到任何错误。
答案 0 :(得分:0)
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;