2

I have searched without success to a similar situation as follows.

I have two lists, list A and list B.
List A is composed of 10 objects created from ClassA which contains only strings.
List B is composed of 100 objects created from ClassB which only contains decimals.

List A is the header information.
List B is the data information.

The relationship between the two lists is:

Row 1 of list A corresponds to rows 1-10 of list B.
Row 2 of list A corresponds to rows 11-20 of list B.
Row 3 of list A corresponds to rows 21-30 of list B.

etc.........

How can I combine these two lists so that when I display them on the console the user will see a header row followed immediately by the corresponding 10 data rows.

I apologize if this has been answered before.

4

4 回答 4

1

这是一些应该满足您的要求的代码 - 我将找到分区扩展的链接,因为我在我的代码中找不到它了:

void Main()
{
    List<string> strings = Enumerable.Range(1,10).Select(x=>x.ToString()).ToList();
    List<decimal> decimals = Enumerable.Range(1,100).Select(x=>(Decimal)x).ToList();

    var detailsRows = decimals.Partition(10)
                              .Select((details, row) => new {HeaderRow = row, DetailsRows = details});

    var headerRows = strings.Select((header, row) => new {HeaderRow = row, Header = header});

    var final = headerRows.Join(detailsRows, x=>x.HeaderRow, x=>x.HeaderRow, (header, details) => new {Header = header.Header, Details = details.DetailsRows});
}

public static class Extensions
{
    public static IEnumerable<List<T>> Partition<T>(this IEnumerable<T> source, Int32 size)
    {
        for (int i = 0; i < Math.Ceiling(source.Count() / (Double)size); i++)
            yield return new List<T>(source.Skip(size * i).Take(size));
    }
}    

该 Partition 方法是完成繁重工作的方法...

这是文章的链接 -链接

编辑 2

这是该Main()方法的更好代码...急于回答并忘记了大脑:

void Main()
{
    List<string> strings = Enumerable.Range(1,10).Select(x=>x.ToString()).ToList();
    List<decimal> decimals = Enumerable.Range(1,100).Select(x=>(Decimal)x).ToList();

    var detailsRows = decimals.Partition(10);

    var headerRows = strings; //just renamed for clarity from other code

    var final = headerRows.Zip(detailsRows, (header, details) => new {Header = header, Details = details});
}
于 2013-09-16T23:20:48.137 回答
1

好的,这应该工作。让我知道,以防我有什么问题。

List<ClassA> listA = GetListA()// ...
List<ClassB> listB = GetListA()// ...


if(listB.Count % listA.Count != 0)     
      throw new Exception("Unable to match listA to listB");

var datasPerHeader = listB.Count / listA.Count;

for(int i = 0; i < listA.Count;i++)
{
    ClassA header = listA[i];
    IEnumerable<ListB> datas = listB.Skip(datasPerHeader*i).Take(datasPerHeader);
    Console.WriteLine(header.ToString());
    foreach(var data in datas)
    {
          Console.WriteLine("\t{0}", data.ToString());
    } 
}
于 2013-09-16T23:21:25.947 回答
1

除非我遗漏了什么,否则这应该很简单。

var grouped = ListA.Select((value, index) => 
  new {
    ListAItem = value,
    ListBItems = ListB.Skip(index * 10).Take(10)
  })
  .ToList();

返回一个可以循环的匿名类型。

foreach (var group in grouped)
{
  Console.WriteLine("List A: {0}", group.Name);
  foreach (var listBItem in group.ListBItems)
  {
    Console.WriteLine("List B: {0}", listBItem.Name);
  {
}
于 2013-09-16T23:47:30.627 回答
0

最简单的方法可能是这样的:

var listA = new List<string>() { "A", "B", "C", ... }
var listB = new List<decimal>() { 1m, 2m, 3m, ... }
double ratio = ((double)listA.Count) / listB.Count;
var results = 
    from i in Enumerable.Range(0, listB.Count)
    select new { A = listA[(int)Math.Truncate(i * ratio)], B = listB[i] };

或流利的语法:

double ratio = ((double)listA.Count) / listB.Count;
var results = Enumerable.Range(0, listB.Count)
    .Select(i => new { A = listA[(int)Math.Truncate(i * ratio)], B = listB[i] });

当然,如果您知道listB中的每个项目总是有 10 个项目listA,您可以将其简化为:

var results = 
    from i in Enumerable.Range(0, listB.Count)
    select new { A = listA[i / 10], B = listB[i] };

或流利的语法:

var results = Enumerable.Range(0, listB.Count)
    .Select(i => new { A = listA[i / 10], B = listB[i] });

这将返回一个结果集,如

{ { "A", 1 }, 
  { "A", 2 }, 
  { "A", 3 }
  ..,
  { "A", 10 },
  { "B", 11 },
  { "B", 12 },
  { "B", 13 },
  ...
  { "B", 20 },
  { "C", 21 },
  ...
  { "J", 100 }
}
于 2013-09-16T23:24:40.177 回答