Taher
Taher

Reputation: 593

LINQ to Entities: Lambda Expression Error

I have 3 radio button Any, Single and Multiple. They are filters on records in my grid view.

Any dont apply any condition on the query.

Single and Multiple apply a check condition on the query, 1 or 2 respectively.

This code has some error but I can't solve it or find a work around.

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);
    }

The error I am getting at Case1 is:

  1. "Instance argument: cannot convert from 'System.Linq.IQueryable' to 'System.Linq.ParallelQuery' "

  2. "'System.Linq.IQueryable' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery, TSource)' has some invalid arguments"

The error I am getting at Case2 is:

" 'short' does not contain a definition for 'SpecID' and no extension method 'SpecID' accepting a first argument of type 'short' could be found (are you missing a using directive or an assembly reference?)"


Notice that I have done something similar to Group filtering but in this case I get no error.

Upvotes: 0

Views: 190

Answers (1)

PulseLab
PulseLab

Reputation: 1579

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;

Upvotes: 1

Related Questions