Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ运算符“==”不能应用于“char”和“string”类型的操作数_C#_Linq - Fatal编程技术网

C# LINQ运算符“==”不能应用于“char”和“string”类型的操作数

C# LINQ运算符“==”不能应用于“char”和“string”类型的操作数,c#,linq,C#,Linq,我过去很少使用LINQ。今天,当我尝试使用LinqPad编写一个小LINQ查询时,出现了以下错误: 运算符“==”不能应用于“char”和“string”类型的操作数 这是我试图写的脚本: void Main() { var csvlines = File.ReadAllLines(@"M:\smdr(backup08-06-2015).csv"); var csvLinesData = csvlines.Skip(1).Select(l =>

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

运算符“==”不能应用于“char”和“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:索引超出了数组的边界


我哪里出错了?

您正在比较字符串和字符类型

替换

其中r1[4]==I/其中r1[4]==O


其中r1[4]=='I'/其中r1[4]=='O'

使用单引号引用“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()
我看到第1个错误已经解决了,使用引号而不是将字符括起来

关于问题中的第二个错误编辑1:从代码中,我假设r1是字符数组

您正在从逗号分隔的文本文件*.CSV文件中读取:

var csvlines = File.ReadAllLines(@"M:\smdr(backup08-06-2015).csv");
虽然列以逗号分隔,但此格式并不强制要求每行中的列数相同。如果使用CR+LF终止该行,则新行开始

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

因此,在这些情况下,字符数组r1的维数较短,并且

IndexOutOfRangeException:索引超出了数组的边界

如果您试图访问r1[4]

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

例如:

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

相应地更改代码,例如:

r1在哪里=null&&r1.Length>=5&&r1[4]=“O”

以确保它不会超出数组的边界。您可以轻松地在中进行测试,或者将下面的代码放入控制台应用程序的主功能中进行测试,也可以很好地运行它:

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&&r1.Length>=5则出现您提到的错误。

我有点惊讶它没有为您转换它,但如果您只执行r1[4]='I'和r1[4]='O'单引号而不是双引号,它是否有效?请查看我编辑的部分,因为我现在遇到了不同的错误。感谢您尝试访问r1中的第13个r1[12]和第5个r1[4]字符。似乎至少有一个r1值没有那么长。您需要处理该场景。如何捕获和处理该情况?使用调试器。在创建数组的地方设置断点,然后一步一步地执行F10并观察长度如何变化…请查看我编辑的部分,因为我现在遇到了不同的错误。谢谢,请查看我编辑的部分,因为我现在遇到了不同的错误。感谢您至少有一个索引似乎无效/达到高r1[4]、l[12]或r[12]调试时间如何捕获和处理这种情况?
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());
    }

}