我过去几乎没用过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;
}
Run Code Online (Sandbox Code Playgroud)
根据建议,我编辑了代码
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();
Run Code Online (Sandbox Code Playgroud)
现在它编译,但它抛出了一个不同的错误:
IndexOutOfRangeException: Index was outside the bounds of the array.
我在哪里弄错了?
你正在比较字符串和字符类型
更换
where r1[4] == "I"/ where r1[4] == "O"
同
where r1[4] == 'I'/ where r1[4] == 'O'