0

Getting Error ->

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.

var checkcategory = from eqpCategory in db.rEqpCategories
                    join eqpType in db.rEqpTypes 
                    on eqpCategory.EqpTypeId equals eqpType.EqpTypeId 
                    join eqpGroup in db.rEqpGroups 
                    on eqpType.EqpGroupId equals eqpGroup.EqpGroupId 
                    where eqpCategory.EqpTypeId == int.Parse(cmbType.SelectedValue) 
                    && eqpCategory.EqpCategoryName == txtCategory.Text 
                    && eqpGroup.EqpGroupId == int.Parse(cmbGroup.SelectedValue)
                    select eqpCategory.EqpCategoryId;


if (checkcategory.Count() > 0)
{
    Page.ClientScript.RegisterClientScriptBlock(typeof(CMSUtility.TransferEquipments), 
                          "warning", 
                          "alert('Category Already present for same Group and Type');", 
                          true);
}
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
  • 1
    Assign `int.Parse` result to some variable before LINQ query and use the variable in it. – Ulugbek Umirov Aug 07 '15 at 10:09
  • 1
    possible duplicate of [LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression](http://stackoverflow.com/questions/13887296/linq-to-entities-does-not-recognize-the-method-int32-int32system-string-meth) – cverb Aug 07 '15 at 10:23
  • Thanks that did the trick.... – Harshit Verma Aug 07 '15 at 10:35

2 Answers2

4

You cannot use int.Parse method inside LINQ query, because it will be tried to convert into database query and will fail. Introduce new variables before the query.

int eqpTypeId = int.Parse(cmbType.SelectedValue);
int eqpGroupId = int.Parse(cmbGroup.SelectedValue);
var checkcategory = from eqpCategory in db.rEqpCategories
                    join eqpType in db.rEqpTypes
                    on eqpCategory.EqpTypeId equals eqpType.EqpTypeId
                    join eqpGroup in db.rEqpGroups
                    on eqpType.EqpGroupId equals eqpGroup.EqpGroupId
                    where eqpCategory.EqpTypeId == eqpTypeId &&
                          eqpCategory.EqpCategoryName == txtCategory.Text &&
                          eqpGroup.EqpGroupId == eqpGroupId
                    select eqpCategory.EqpCategoryId;
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31
  • It should be noted that, even if having the Parse() in the linq query DID work, it would still be a good idea to move it outside, so the parse isn't repeated for every row in the table. – James Curran Aug 07 '15 at 14:25
1

You are not allowed to run int.Parse in a EF Linq query. Try this

var id = int.Parse(cmbGroup.SelectedValue);
var checkcategory = from eqpCategory in db.rEqpCategories
                    join eqpType in db.rEqpTypes 
                         on eqpCategory.EqpTypeId equals      eqpType.EqpTypeId 
                    join eqpGroup in db.rEqpGroups 
                         on eqpType.EqpGroupId equals eqpGroup.EqpGroupId 
                    where eqpCategory.EqpTypeId == id 
                    && eqpCategory.EqpCategoryName == txtCategory.Text 
                    && eqpGroup.EqpGroupId == id
                    select eqpCategory.EqpCategoryId;

                    if (checkcategory.Count() > 0)
                    {
                        Page.ClientScript.RegisterClientScriptBlock(typeof(CMSUtility.TransferEquipments), "warning", "alert('Category Already present for same Group and Type');", true);
                    }
Aron
  • 15,464
  • 3
  • 31
  • 64