字符串转换为INT在LINQ [英] Convert String To Int in LINQ

查看:207
本文介绍了字符串转换为INT在LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LINQ查询,查询的数据表。在数据表,字段是一个字符串,我需要比较,为一个整数,基本上是:

I have a LINQ query that queries a DataTable. In the DataTable, the field is a string and I need to compare that to an integer, basically:

if ((electrical >= 100 && electrical <= 135) || electrical == 19)
{
    // The device passes
}

问题是,我试图做到这一点的LINQ是这样的:

the problem is, I am trying to do this in LINQ like this:

        var eGoodCountQuery = 
            from row in singulationOne.Table.AsEnumerable()
            where (Int32.Parse(row.Field<String>("electrical")) >= 100 &&
                   Int32.Parse(row.Field<String>("electrical")) <= 135) &&
                   Int32.Parse(row.Field<String>("electrical")) != 19 &&
                   row.Field<String>("print") == printName
            select row;

我不断收到异常:

I keep getting the exception:

输入字符串的不正确的格式

Input string was not in a correct format

主要的问题发生在电子==

The main problem occurs when electrical == ""

推荐答案

我无法得到任何工作,所以我重新做了整个方法:

I could not get anything to work, so I re-did the whole method:

    public bool GetElectricalStatus(string printName)
    {
        List<object> eGoodList = new List<object>();
        var eGoodCountQuery =
            from row in singulationOne.Table.AsEnumerable()
            where row.Field<String>("print") == printName
            select row.Field<String>("electrical");

        foreach (var eCode in eGoodCountQuery)
        {
            if (!string.IsNullOrEmpty(eCode.ToString()))
            {
                int? eCodeInt = Convert.ToInt32(eCode);
                if (eCodeInt != null &&
                    (eCodeInt >= 100 && eCodeInt <= 135) || eCodeInt == 19)
                {
                    eGoodList.Add(eCode);
                }
            }
        }
        if (eGoodList.Count() > 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

主要的问题发生在电子==

The main problem occurs when electrical == ""

这篇关于字符串转换为INT在LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆