4

我有一个字符串列表:

{"foo", "str1", "str2", ..., "bar", ..., "baz", ...}

我需要获取"foo","bar"和之间的字符串子列表"baz"

可以用linq做到这一点吗?

编辑
我需要一种方法,而无需两次查看列表。

4

3 回答 3

6

您可以这样做来细化任何两个其他元素之间的所有元素:

var strings = new[] { "foo", "str1", "str2", ... "bar", ... "baz" };
var between = strings.SkipWhile(s => s != "foo").Skip(1)
                     .TakeWhile(s => s != "bar"); // "str1", "str2", ...

如果你想得到“foo”和“baz”之间的所有东西,除了“bar”,使用这个(假设顺序“foo”,“bar”,“baz”):

var strings = new[] { "foo", "str1", "str2", ... "bar", ... "baz" };
var between = strings.SkipWhile(s => s != "foo").Skip(1)
                     .TakeWhile(s => s != "baz")
                     .Where(s => s != "bar"); // "str1", "str2", ...

或者,如果您对使用具有副作用的 Linq 查询感到满意,您可以这样做以通过某些“停止”词对输入列表进行分区:

 var stops = new[] { "foo", "bar", "baz" };
 var strings = new[] { "foo", "str1", "str2", "bar", "str3", "baz" };
 var p = -1;
 var partitions = 
     from s in strings
     let i = Array.IndexOf(stops, s) 
     group s by p = i == -1 ? p : i into g
     where g.Key == 0 || g.Key == 1
     select g.Skip(1); // { "str1", "str2" }, { "str3" }

或者效率更高一些(因为它在第三个停用词之后停止处理):

 var partitions = 
     (from s in strings
      let i = Array.IndexOf(stops, s) 
      group s by p = i == -1 ? p : i)
     .SkipWhile(g => g.Key < 0)
     .Take(2)
     .Select(g => g.Skip(1)); // { "str1", "str2" }, { "str3" }

现在,这种方法在边缘有点粗糙,当涉及到“foo”之前或“baz”之后的项目时,它有点繁琐,但是如果你只寻找“foo”和“baz”之间的项目,它应该适合你。它的另一个好处是停用词的顺序不会影响结果。

于 2013-06-11T14:14:35.173 回答
4
var idxFoo = list.IndexOf("foo");
var idxBar = list.IndexOf("bar");
var idxBaz = list.IndexOf("baz");

var subList1 = list.Skip(idxFoo).Take(idxBar - idxFoo);
var subList2 = list.Skip(idxBar).Take(idxBaz - idxBar);
于 2013-06-11T14:12:10.520 回答
0

如果您只想通过广泛的数据列表迭代一次,您可以这样做:

List<string> longDataList = new List<string> { "foo", "str1", "str2", "str1", "str2", "str1", "str2", "bar", "str1", "str2", "str1", "str2", "str1", "str2", "baz", "str1", "str2", "str1", "str2", "str1", "str2" };
List<string> splitters = new List<string> { "foo", "bar", "baz" };
Dictionary<string, List<string>> resultDict = new Dictionary<string, List<string>>();
List<string> currentList = null;
longDataList.ForEach(s =>
    {
       if (splitters.Contains(s))
          {
            if (resultDict.ContainsKey(s))
                currentList = resultDict[s];
             else
                {
                 currentList = new List<string>();
                  resultDict.Add(s, currentList);
                }
           }
        else
           currentList.Add(s);
     });

至少使用一点 linq,但这个技巧只在你的大量数据列表中迭代一次。

于 2013-06-11T14:31:59.723 回答