当数据来自 CSV 文件时,如何按 LINQ 筛选数据



在这里,我给出了我的csv文件如何包含数据的想法

Call start       Call duration    Ring duration       Direction     Is_Internal   Continuation    Party1Name
----------      -------------     -------------       ---------     -----------  ------------    -----------   
09-06-15 7:27     0:00:06           0                   I              1             1             ACC
09-06-15 7:27     0:00:06           0                   I              0             1             ACC
09-06-15 11:27    0:00:06           0                   )              0             1             Sales
09-06-15 7:27     0:00:06           0                   I              0             1             ACC
09-06-15 7:27     0:00:06           0                   I              1             0             Suzzy
09-06-15 03:27    0:00:06           0                   I              0             1             Suzzy
09-06-15 7:27     0:00:06           0                   I              0             1             ACC
09-06-15 7:27     0:00:06           0                   O              0             0             Sales
09-06-15 7:27     0:00:06           0                   I              1             1             ACC
09-06-15 12:27    0:00:06           0                   I              0             1             Matt
09-06-15 10:27    0:00:06           0                   I              0             1             VM Channel
09-06-15 7:27     0:00:06           0                   )              0             0             VM Channel
09-06-15 7:27     0:00:06           0                   I              0             1             Voice Mail

现在我想显示员工明智的数据,如下所示

CSR Name       Incomming      outgoing    call transfer
--------       ----------     --------    -------------
ACC               10               12           11
SALES             05               06           02
Suzy              7                 5            5
Matt              2                 2            2

我的条件是
1)对于传入方向必须是I,对于传出方向将是O
2) 员工姓名 VM 通道和语音邮件将不予考虑

等等

我不擅长 LINQ,

这就是为什么发布这个问题寻求帮助的原因。 我有 LINQ 查询示例,它有点相似,但仍然不知道在其中纠正或添加什么以获得我想要的输出。

这是 LINQ 代码

void Main()
{
    var csvlines = File.ReadAllLines(@"M:smdr(backup08-06-2015).csv");   
    var csvLinesData = csvlines.Skip(1).Select(l => l.Split(',').ToArray());
    //int iDir = csvLinesData.Count(w => w.direction='I');
    var users = csvLinesData.Select(data => new User
    {
                CSRName =   data[12],
                Incomming = csvLinesData.Count(w => w[4] == "I"),
                outgoing = csvLinesData.Count(w => w[4] == "O")
    }).ToList();
    users.Dump();
}
class User
{
    public string CSRName;
    public int outgoing;
    public int Incomming;
    public int calltransfer;
}

如何从结果中删除员工姓名 VM 通道和语音邮件。 在SQL中,我们可以在子句中使用not like和not in 子句,但是在LINQ中有哪些类似的东西?

如何获取员工姓名明智的数据

CSRName = data[12],
Incomming = csvLinesData.Count(w => w[4] == "I")

如果此行CSRName = data[12]返回 Suzy,则与 Suzy 相关的传入数据将存储在Incomming变量中。 如何实现它。

只是想知道是否有人可以帮助 LINQ 查询。

我已经尝试了一下,得到了下面的代码。但我不知道这有多大用处,因为我不太擅长 LINQ。如果这不起作用,您可以使用我的解决方案并制作更好的解决方案:)

var csvlines = File.ReadAllLines(@"M:smdr(backup08-06-2015).csv");
var csvLinesData = csvlines.Skip(1).Select(l => l.Split(',').ToArray());
// i am assuming that line[7] is the Party1Name Column
// now you have a (sorted) group with n "members" (ACC, Sales, ..., n )
var groupOfUser = from line in csvLinesData 
                  group line by line[7] into newGroup 
                  orderby newGroup.Key 
                  select newGroup;
// The Key of your userOfGrp is the Name e.g. "ACC"
// i am assuming that x[4] is the direction Column
// I count all I or O and put them into the new User
var user = (from userOfGrp in groupOfUser
            select
                new User()
                    {
                        CSRName = userOfGrp.Key,
                        Incomming = userOfGrp.Count(x => x[4] == "I"),
                        Outgoing = userOfGrp.Count(x => x[4] == "O")
                    }).ToList();

组队列是从 MSDN 复制的。你可以看看这个

但是,为什么需要 LINQ?其他解决方案也可以很棒!

感谢帮助我构造以下 linq 查询的@Jens。 我发布了我的完整代码,它工作正常。 谢谢

void Main()
{
    var csvlines = File.ReadAllLines(@"M:smdr(backup08-06-2015).csv");
var csvLinesData = csvlines.Skip(1).Select(l => l.Split(',').ToArray());
// i am assuming that line[7] is the Party1Name Column
// now you have a (sorted) group with n "members" (ACC, Sales, ..., n )
var groupOfUser = from line in csvLinesData 
                  where  !line[12].Contains("VM") && !line[12].Contains("Voice Mail")
                  group line by line[12] into newGroup 
                  orderby newGroup.Key 
                  select newGroup;
// The Key of your userOfGrp is the Name e.g. "ACC"
// i am assuming that x[4] is the direction Column
// I count all I or O and put them into the new User
var user = (from userOfGrp in groupOfUser
            select
                new User()
                    {
                        CSRName = userOfGrp.Key,
                        Incomming = userOfGrp.Count(x => x[4] == "I"),
                        Outgoing = userOfGrp.Count(x => x[4] == "O")
                    }).ToList();
                    user.Dump();
}
class User
{
    public string CSRName;
    public int Outgoing;
    public int Incomming;
    public int calltransfer;
}

相关内容

  • 没有找到相关文章

最新更新