【问题标题】:How to filter data by LINQ when data is coming from CSV file当数据来自CSV文件时如何通过LINQ过滤数据
【发布时间】:2015-06-16 14:51:26
【问题描述】:

这里我想知道我的 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) 员工姓名虚拟机频道和语音邮件将不予考虑

还有更多

我不擅长 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] return suzy 那么 suzy 相关的传入数据将存储在Incomming 变量中。如何实现。

只是想知道是否有人可以帮助进行 linq 查询。

【问题讨论】:

  • 您的问题归结为“如何使用 LINQ 进行过滤?”。到目前为止,您尝试过什么?
  • 这可能会对您有所帮助:LINQ to CSV library.
  • 我想用 LINQ 实现它............请不要第三方。

标签: c# linq csv


【解决方案1】:

我尝试了一下,得到了下面的代码。 但我不知道这有多少工作,因为我不是那么擅长 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 查询中缺失。 “如何从结果中删除员工姓名 VM 频道和语音邮件。在 sql 中我们可以使用 not like 和 not in 子句,但在 linq 中有哪些类似的东西可用?”执行 CSR 名称(如“语音邮件”并以“VM”开头)的代码在哪里?
  • 你能在使用 lamda 写相同查询的地方写第二组答案吗?
  • "in LINQ",这是 LINQ :D 但我可以看看我是否可以在查询 + lamda - 明天做到这一点
【解决方案2】:

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

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;
}

【讨论】:

    猜你喜欢
    • 2018-06-09
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 2018-05-17
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多