【问题标题】:Linq query formation errorLinq 查询形成错误
【发布时间】:2015-10-30 17:54:12
【问题描述】:

出现错误 ->

LINQ to Entities 无法识别方法 'Int32 ToInt32(System.String)' 方法,这个方法不能翻译 到商店表达式中。

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

【问题讨论】:

标签: c# linq


【解决方案1】:

不允许在 EF Linq 查询中运行 int.Parse。试试这个

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

【讨论】:

    【解决方案2】:

    您不能在 LINQ 查询中使用int.Parse 方法,因为它将尝试转换为数据库查询并且会失败。在查询之前引入新变量。

    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;
    

    【讨论】:

    • 应该注意的是,即使 linq 查询中的 Parse() DID 工作,将它移到外面仍然是一个好主意,因此不会对每一行重复解析在表中。
    猜你喜欢
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多