【问题标题】:LINQ Operator '==' cannot be applied to operands of type 'char' and 'string'LINQ 运算符“==”不能应用于“char”和“string”类型的操作数
【发布时间】:2015-06-17 13:10:45
【问题描述】:

过去我几乎不使用 LINQ。今天尝试使用 LinqPad 编写一个小 LINQ 查询时,出现以下错误:

Operator '==' cannot be applied to operands of type 'char' and 'string'

这是我试图编写的脚本:

void Main()
{
            var csvlines = File.ReadAllLines(@"M:\smdr(backup08-06-2015).csv");
            var csvLinesData = csvlines.Skip(1).Select(l => l.Split(',').ToArray());
            var csvData = csvLinesData.Where(l => (!l[12].Contains("VM") && l[12] != "Voice Mail")).ToArray();
            var user = (from r in csvData
                        orderby r[12]
                        select new User
                        {
                            CSRName = r[12],

                            Incomming = (from r1 in r
                                         where r1[4] == "I"
                                         select r1).Count(),
                            outgoing = (from r1 in r
                                        where r1[4] == "O"
                                        select r1).Count()



                        }).ToList();
                        user.Dump();
}

class User
{
    public string CSRName;
    public int Outgoing;
    public int Incomming;
    public int calltransfer;
}

编辑 1

根据建议我编辑了代码

                    select new User 
                    {
                        CSRName=r[12],
                        Incomming=(from r1 in r
                                  where r1[4]=='I'
                                  select r1).Count(),
                        outgoing = (from r1 in r
                                    where r1[4] == 'O'
                                    select r1).Count()

                    }).ToList();

现在它可以编译了,但它抛出了一个不同的错误:

IndexOutOfRangeException: Index was outside the bounds of the array.

我在哪里犯错了?

【问题讨论】:

  • 我有点惊讶它没有为你转换它,但如果你只做 r1[4] == 'I'r1[4] == 'O' (单引号而不是双引号)它是否有效?
  • 请查看我编辑的部分,因为我现在遇到不同的错误。谢谢
  • 您正在尝试访问r1 中的第 13 个 (r1[12]) 和第 5 个 (r1[4] 字符。看来 r1 的至少一个值没有那么长。您需要处理这种情况。
  • 如何捕捉和处理这种情况?
  • 使用调试器。在创建数组的地方设置断点,然后一步一步(F10)观察长度如何变化...

标签: c# linq


【解决方案1】:

你在比较字符串和字符类型

替换

where r1[4] == "I"/where r1[4] == "O"

where r1[4] == 'I'/where r1[4] == 'O'

【讨论】:

  • 请查看我编辑的部分,因为我现在遇到不同的错误。谢谢
  • 您的至少一个索引似乎无效/高r1[4]l[12]r[12] 调试时间
  • 如何捕捉和处理这种情况?
【解决方案2】:

使用单引号引用'char':

Incomming = (from r1 in r
    where r1[4] == 'I'
    select r1).Count(),
    outgoing = (from r1 in r
        where r1[4] == 'O'
        select r1).Count()

【讨论】:

  • 请查看我编辑的部分,因为我现在遇到不同的错误。谢谢
【解决方案3】:

我看到第一个错误已经解决了(使用'引号而不是"将字符括起来)。

关于您的 第二个错误(您的问题中的“编辑 1”):根据您的代码,我假设 r1 是一个字符数组。

您正在读取逗号分隔的文本文件(“*.CSV 文件”):

var csvlines = File.ReadAllLines(@"M:\smdr(backup08-06-2015).csv");

虽然列是用逗号分隔的,但这种格式并不强制您在每行中拥有相同数量的列。如果您已用 CR+LF 终止该行,则会开始一个新行。

但在您提供的源代码中,您假设*.CSV 文件中的每一行 (r1) 都需要包含 5 个字符,但情况并非总是如此。

因此,在这些情况下,字符数组 r1 的维度会更短,而您会得到

IndexOutOfRangeException: Index was outside the bounds of the array.

如果您尝试访问r1[4]

为了说明这一点,我给出一个

示例:

char[] r = { 'a', 'b', 'c', 'd' };
Console.WriteLine(r[2]); // succeeds
Console.WriteLine(r[3]); // succeeds
Console.WriteLine(r[4]); // fails: allowed index range is 0..3

使用r.Length获取数组的长度:在示例中为4,索引从0开始,因此允许范围为0..3。

相应地更改您的代码,例如:

where (r1!=null && r1.Length>=5) && (r1[4] == 'O')

以确保它不会超出数组的范围。您可以在LinqPad 中轻松测试它(或者将下面的代码放入控制台应用程序的 Main 函数中进行测试,DotNetFiddle 也可以很好地运行它):

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
            var r = new List<char[]> { new char[]{ 'a', 'b', 'c', 'd' }, 
                new char[]{ 'a', 'b', 'c', 'd', 'O' } };

            Console.WriteLine((from r1 in r
            where (r1!=null && r1.Length>=5) && r1[4] == 'O'
            select r1).Count());
    }

}

如果您删除表达式(r1!=null &amp;&amp; r1.Length&gt;=5),则会出现您提到的错误。

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    相关资源
    最近更新 更多