0

I got a list of strings,on which I would like to do an operation which concats each item with rest of the items. The below test fails at the moment, I think the join is not the correct linq method I should be using- can you please let me know how to get this done? The pattern of the output should tell how the projection should be,if not the rule is simple:take one item and concatenate with all the other items and then move to the next item.Test below:

[Test]
        public void Should_concatenate_all_items()
        {
            var items = new List<string> {"a", "b", "c", "d"};

            var concatenatedList = items .Join(items , x => x, y => y, (x, y) => string.Concat(x, y));

            foreach (var item in concatenatedList)
            {                
                //Should output:ab
                //Should output:ac
                //Should output:ad
                //Should output:bc
                //Should output:bd
                //Should output:cd
                Console.WriteLine(item);
            }
        }   

Note: I'm using .NET 3.5.

4

3 回答 3

3

你可以使用这样的东西:

var concatenatedList =
    from x in items.Select((str, idx) => new { str, idx })
    from y in items.Skip(x.idx + 1)
    select x.str + y;

或流利的语法:

var concatenatedList =
    items.Select((str, idx) => new { str, idx })
         .SelectMany(x => items.Skip(x.idx + 1), (x, y) => x.str + y);
于 2013-10-15T23:18:56.190 回答
0
var concatenatedList = (from i in items
                     from x in items
                     where i != x
                     select string.Concat(i, x)).ToList();

对于您列出的确切输出:

 var concatenatedList = (from i in items
                     from x in items
                     where i != x && items.IndexOf(i) < items.IndexOf(x)
                     select string.Concat(i, x)).ToList();
于 2013-10-15T23:30:48.880 回答
0

我认为我的解决方案可能有点矫枉过正,但在现实世界中它可能更有帮助。另外,我在 Linq 中找不到干净的方法。Linq 的 except 似乎不适合这个。

您可以使用 IEnumerator 为您枚举值。

public class ConcatEnum : IEnumerator
{
    public List<String> _values;

    // Enumerators are positioned before the first element 
    // until the first MoveNext() call. 
    int position1 = 0;
    int position2 = 0;

    public ConcatEnum(List<String> list)
    {
        _values = list;
    }

    public bool MoveNext()
    {
        position2 = Math.Max(position2 + 1, position1 + 1);
        if (position2 > _values.Count - 1){
            position1++;
            position2 = position1 + 1;
        }

        return position1 < _values.Count - 1;
    }

    public void Reset()
    {
        position1 = 0;
        position2 = 0;
    }

    object IEnumerator.Current
    {
        get
        {
            return Current;
        }
    }

    public String Current
    {
        get
        {
            try
            {
                return _values[position1] + _values[position2];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }

    public IEnumerator GetEnumerator()
    {
        this.Reset();
        while (this.MoveNext())
        {
            yield return Current;
        }
    }
}

像这样称呼它:

var items = new List<string> { "a", "b", "c", "d" };
foreach (var i in new ConcatEnum(items))
{
    //use values here
}
于 2013-10-15T23:40:36.127 回答