简体   繁体   English

LINQ:如何添加多个字段进行分组

[英]LINQ: How to add multiple fields for grouping

this code apply grouping on party name whose ordinal position is 12 此代码对顺序位置为12的当事方名称进行分组

group line by line[12] into newGroup 

now i want to know how to apply group on multiple field whose ordinal position is 0 means line[0] 现在我想知道如何在顺序位置为0的多个字段上应用组表示line[0]

line[0] will have date and time both. line[0]行将同时具有日期和时间。 i just like to group on time part. 我只是喜欢按时分组。 this way i can extract time portion 这样我可以提取时间部分

DateTime.Parse(line[0].Substring(line[0].Length-8))

var csvlines = File.ReadAllLines(@"M:\smdr(backup08-06-2015).csv");
var csvLinesData = csvlines.Skip(1).Select(l => l.Split(',').ToArray());

var groupOfUser = from line in csvLinesData 
                  group line by line[12] into newGroup 
                  orderby newGroup.Key 
                  select newGroup;

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();

1) just help me to group on multiple field one is line[12] and other one is line[0] 2) line[0] will have date and time portion like "2015/06/08 07:59:12" and i have to apply group on only time portion on line[0] 1)只是帮助我在多个字段上分组,一个是line [12],另一个是line [0] 2) line[0]将具有日期和时间部分,例如“ 2015/06/08 07:59:12”,并且我只需要在line[0]时间部分上应用组

EDIT 1 编辑1

this line group r by new { prop1 = r[12], ((DateTime)r[0]).TimeOfDay } into g throwing error Cannot convert type 'string' to 'System.DateTime' 该行group r by new { prop1 = r[12], ((DateTime)r[0]).TimeOfDay } into g抛出错误无法将类型'string'转换为'System.DateTime'

void Main()
{
    var csvlines = File.ReadAllLines(@"c:\smdr.csv");
            var csvLinesData = csvlines.Skip(1).Select(l => l.Split(',').ToArray());
            var csvData = csvLinesData.Where(l => (l[6] != "VM Channel" && l[6] != "Voice Mail")).ToArray();
            var user = (from r in csvData
                        group r by new { prop1 = r[12], Time = ((DateTime)r[0]).TimeOfDay } into g
                        orderby g.Count()
                        select new User 
                        {
                            CSRName=g.Key,
                            Incomming=(from r1 in g
                                      where r1[3]=="I"
                                      select r1).Count(),
                            outgoing = (from r1 in g
                                        where r1[3] == "O"
                                        select r1).Count()

                        }).ToList();
}

class User
{
    public string CSRName;
    public int outgoing;
    public int Incomming;

}

If I understand you well, you need this: 如果我对您的了解很好,那么您需要:

from line in csvLinesData 
group line by new 
              { 
                  prop1 = line[12],
                  Time = DateTime.Parse(line[0]).TimeOfDay
               } into newGroup 
orderby newGroup.Key.prop1, newGroup.Key.Time 
select newGroup;

Since line[0] is a string (apparently), you have to parse it to a DateTime value. 由于line[0]是一个字符串(显然),因此您必须将其解析为DateTime值。 I show the most simple method to do this, but maybe you should build in some more safety because not each string can be parsed to a DateTime . 我展示了执行此操作的最简单方法,但也许您应该建立一些更安全的方法,因为并非每个字符串都可以解析为DateTime But that's beyond the scope of this question. 但这超出了这个问题的范围。

Note that the anonymous type has named properties, you probably want to rename prop1 . 请注意,匿名类型具有命名属性,您可能想重命名prop1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM