使用LINQ根据它们将包含的已知子字符串对字符串列表进行分组 [英] Using LINQ to group a list of strings based on known substrings that they will contain

查看:104
本文介绍了使用LINQ根据它们将包含的已知子字符串对字符串列表进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已知的字符串列表,如下所示:

  List< string> groupNames = new List< string>(){Group1,Group2,Group3}; 

我也有一个事先不知道的字符串列表,它会是这样的: / p>

 列表< string> dataList = new List< string>()
{
Group1.SomeOtherText,
Group1.SomeOtherText2,
Group3.MoreText,
Group2 .EvenMoreText
;

我想要做一个LINQ语句,它将取得dataList并将其转换为匿名对象或一个包含组名的Key的字典和一个包含该组中字符串列表的Value。为了循环遍历组和内部循环遍历组列表,并根据它所在的组对字符串进行不同的操作。



我想要一个数据结构如下所示:

  var groupped = new 
{
new
{
Key =Group1,
DataList = new List< string>()
{
Group1.SomeOtherText,
Group1.SomeOtherText2




Key =Group2
DataList = new List< string>()
{
Group2.EvenMoreText
}
}
...
};

我知道我可以循环访问dataList,然后检查每个字符串是否包含组名,然后添加他们到个人名单,但我正在尝试学习LINQ做这样的任务的方式。



预先致谢。

编辑:

刚刚有另一个想法...如果我的组名称是在一个枚举?

  public enum组
{
Group1,
Group2,
Group3
}

我怎样才能把它放到字典中?



这就是我正在尝试的,但我不确定如何形成ToDictionary部分

 字典<组,列表<字串>> grouppedDictionary =(来自Enum.GetNames中的groupName(typeof(Groups))
来自dataList中的数据
其中data.Contains(groupName)
组数据by groupName).ToDictionary< Groups,List<字符串>>(...不知道该在哪里放....);

编辑2:

找到解决方案到Enum问题:

  var enumType = typeof(Groups); 
词典<组,列表<字串>>查询=(来自Enum.GetValues(enumType)中的groupName .Cast< Groups>()
来自dataList中的数据
其中data.Contains(Enum.GetName(enumType,groupName))
group data by groupName).ToDictionary(x => x.Key,x => x.ToList());


解决方案

b

  var query = from groupName in groupNames 
from dataList data
where data.StartsWith(groupName)
group data by团队名字;

请注意,此不是连接,因为可能存在重叠例如组名称G和Gr,因此一个项目可以匹配多个组名称。如果您可以从每个项目中提取组名称(例如通过在第一个点之前采取所有操作),那么您可以使用join ... into来获得组加入。无论如何... ...

然后:

  foreach(var result in查询)
{
Console.WriteLine(Key:{0},result.Key);
foreach(var结果中的项目)
{
Console.WriteLine(+ item);


如果你确实需要匿名类型,你可以做...

  var query = from groupName in groupNames 
from dataList in dataList
where data.StartsWith(groupName)
group group by groupName into g
select new {g.Key,DataList = g.ToList()};


I have a known list of strings like the following:

List<string> groupNames = new List<string>(){"Group1","Group2","Group3"};

I also have a list of strings that is not known in advance that will be something like this:

List<string> dataList = new List<string>()
{
   "Group1.SomeOtherText",
   "Group1.SomeOtherText2",
   "Group3.MoreText",
   "Group2.EvenMoreText"
};

I want to do a LINQ statement that will take the dataList and convert it into either an anonymous object or a dictionary that has a Key of the group name and a Value that contains a list of the strings in that group. With the intention of looping over the groups and inner looping over the group list and doing different actions on the strings based on which group it is in.

I would like a data structure that looks something like this:

var grouped = new
{
   new
   {
       Key="Group1",
       DataList=new List<string>()
            {
               "Group1.SomeOtherText",
               "Group1.SomeOtherText2"
            }
    },
    new 
    {
       Key="Group2",
       DataList=new List<string>()
            {
               "Group2.EvenMoreText"
            }
    }
    ...
};

I know I can just loop through the dataList and then check if each string contains the group name then add them to individual lists, but I am trying to learn the LINQ way of doing such a task.

Thanks in advance.

EDIT:

Just had another idea... What if my group names were in an Enum?

public enum Groups
{
    Group1,
    Group2,
    Group3
}

How would I get that into a Dictionary>?

This is what I am trying but i am not sure how to form the ToDictionary part

Dictionary<Groups,List<string>> groupedDictionary =   (from groupName in Enum.GetNames(typeof(Groups))
                                                      from data in dataList 
                                                      where data.Contains(groupName)
                                                      group data by groupName).ToDictionary<Groups,List<string>>(...NOT SURE WHAT TO PUT HERE....);

EDIT 2:

Found the solution to the Enum question:

var enumType = typeof(Groups);
Dictionary<Groups,List<string>> query = (from groupName in Enum.GetValues(enumType).Cast<Groups>()
             from data in dataList
             where data.Contains(Enum.GetName(enumType, groupName))
             group data by groupName).ToDictionary(x => x.Key, x=> x.ToList());

解决方案

That looks like:

var query = from groupName in groupNames
            from data in dataList
            where data.StartsWith(groupName)
            group data by groupName;

Note that this isn't a join, as potentially there are overlapping group names "G" and "Gr" for example, so an item could match multiple group names. If you could extract a group name from each item (e.g. by taking everything before the first dot) then you could use "join ... into" to get a group join. Anyway...

Then:

foreach (var result in query)
{
    Console.WriteLine("Key: {0}", result.Key);
    foreach (var item in result)
    {
        Console.WriteLine("  " + item);
    }
}

If you really need the anonymous type, you can do...

var query = from groupName in groupNames
            from data in dataList
            where data.StartsWith(groupName)
            group data by groupName into g
            select new { g.Key, DataList = g.ToList() };

这篇关于使用LINQ根据它们将包含的已知子字符串对字符串列表进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆