7

我有以下代码来复制列表X时间的成员。

虽然它有效,但感觉不是特别干净。

实时代码示例:http ://rextester.com/UIVZVX7918

public static List<ServiceEndPoint> GetServiceEndPoints()
{
    const string source = "http://webSiteA.asmx,http://webSiteB.asmx";
    const int instances = 3;

    var splitEndPoints =  source.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                                    .Select((s, i) => new ServiceEndPoint
                                                                {
                                                                    Index = i,
                                                                    Uri = s
                                                                })
                                                                .ToList();

    // Duplicate the contents of splitEndPoints "instances" number of times
    var serviceEndPoints = new List<ServiceEndPoint>();
    foreach (var point in splitEndPoints)
    {
        for (var i = 0; i < instances; i++)
        {
            serviceEndPoints.Add(point);
        }
    }

    return serviceEndPoints;
}

public class ServiceEndPoint
{
    public int Index { get; set; }
    public string Uri { get; set; }
}

有更好的方法吗?

4

3 回答 3

18

也许是这样的:

var serviceEndPoints = splitEndPoints.SelectMany(t =>
    Enumerable.Repeat(t, instances)).ToList();

这会给你“A,A,A,B,B,B,C,C,C”。如果你想要“A,B,C,A,B,C,A,B,C”:

var serviceEndPoints = Enumerable.Repeat(
    splitEndPoints, instances).SelectMany(t => t).ToList();
于 2013-08-06T12:27:43.147 回答
10

你可以用一点 Linq 做到这一点:

int instances = 3;
var serviceEndPoints = 
    (from e in Enumerable.Range(0, instances)
     from x in serviceEndPoints
     select x)
    .ToList();

或者,如果您更喜欢流畅的语法:

var serviceEndPoints = Enumerable
    .Range(0, instances)
    .SelectMany(e => serviceEndPoints)
    .ToList();

请注意,给定一个 list like{ A, B, C }将产生一个 list like { A, B, C, A, B, C, A, B, C }。如果你想生成一个类似的列表{ A, A, A, B, B, B, C, C, C },你可以简单地颠倒集合的顺序:

var serviceEndPoints = 
    (from x in serviceEndPoints
     from e in Enumerable.Range(0, instances)
     select x)
    .ToList();

或流利的语法:

var serviceEndPoints = serviceEndPoints
    .SelectMany(x => Enumerable.Range(0, instances), (x, e) => x)
    .ToList();
于 2013-08-06T12:24:46.470 回答
1

拆分两个组件...

var parts = source.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);

var serviceEndPoints = (from i in Enumerable.Range(0, instances * parts.Length)
                        let j = i / instances
                        let part = parts[j]
                        select new ServiceEndPoint { Index = j, Uri = part }).ToList();

或者...

var parts = source.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);

var serviceEndPoints2 = (from i in Enumerable.Range(0, parts.Length)
                        let part = parts[i]
                        from j in Enumerable.Range(0, instances)                            
                        select new ServiceEndPoint { Index = i, Uri = part }).ToList();

for它与另一个里面的两个非常相似:-)

于 2013-08-06T12:32:58.907 回答