2013-07-23 64 views
2

我有Channels如何旋轉這個對象列表?

var blocks = new List<Block>(); 
foreach (var channel in ChannelsCollection) 
{ 
    var ch = channel.ChannelCollectionItems.Select(item => new Channel 
    { 
     Id = channel.Id, 
     Delay = item.Delay, 
     Trigger = item.Trigger, 
     Restore = item.Restore, 
    }).ToList(); 

    blocks.Add(new Block { Id = index++ , Channels = ch}); 
} 

這導致Blocks列表的列表,每個都包含的Channels列表。

例如

Block1   Block2   Block3   Block4 
    Channel1  Channel1  Channel1  Channel1 
     Val1   Val1   Val1   Val1 
     Val2   Val2   Val2   Val2 
     Val3   Val3   Val3   Val3 
    Channel2  Channel2  Channel2  Channel2 
     Val1   Val1   Val1   Val1 
     Val2   Val2   Val2   Val2 
     Val3   Val3   Val3   Val3 
    Channel3  Channel3  Channel3  Channel3 
     Val1   Val1   Val1   Val1 
     Val2   Val2   Val2   Val2 
     Val3   Val3   Val3   Val3 

我需要讓我的Channels包含每個Block該頻道列表,轉動這個名單。

例如

Channel1  
    Block1  Block2  Block3   
     Val1   Val1   Val1 
     Val2   Val2   Val2 
     Val3   Val3   Val3 

Channel2 
    Block1  Block2  Block3   
     Val1   Val1   Val1 
     Val2   Val2   Val2 
     Val3   Val3   Val3    

Channel3 
    Block1  Block2  Block3   
     Val1   Val1   Val1 
     Val2   Val2   Val2 
     Val3   Val3   Val3   

有沒有一種快速的方法(例如用LINQ)來做到這一點?

編輯

ChannelBlock被定義爲:

public class Channel 
{ 
    public int? Id { get; set; } 
    public string Delay { get; set; } 
    public string Trigger { get; set; } 
    public string Restore { get; set; } 
} 

public class Block 
{ 
    public int Id { get; set; } 
    public List<Channel> Channels { get; set; } 
} 
+1

你可以發佈自己的頻道和您的Block類? –

+0

我加了他們。謝謝。 – DaveDev

+1

渠道是否具有區塊集合屬性? –

回答

0

事實證明,這是解決方案:

public class ChannelRowCell 
{ 
    public string Name { get; set; } 
    public string Delay { get; set; } 
    public string Trigger { get; set; } 
    public string Restore { get; set; } 
} 

public class ChannelRow 
{ 
    public int Id { get; set; } 
    public List<ChannelRowCell> Cells { get; set; } 
} 

public static class DigiChannelsExtensions 
{ 

    public static List<ChannelRow> GetRows(this List<DigiChannelsVM> digiChannelsVms) 
    { 
     var blocks = (from vm in digiChannelsVms 
         let ch = vm.ChannelCollectionItems.Select(item => new 
         { 
          vm.Id, 
          Type = item.Types.EnumDataItems.FirstOrDefault(xx => xx.IsSet).Description, 
          Delay = item.Delay.Value.ToString(), 
          Trigger = item.Trigger.Value == 0 ? "+" : "-", 
          Restore = item.Restore.GetSelectedEnumItems().FirstOrDefault().Description == "Restore" ? "Y" : "N", 
         }).ToList() 
         select new { Id = vm.Id.Value, Channels = ch }).ToList(); 

     var channelRows = new List<ChannelRow>(); 
     //for (var i = 0; i < blocks[0].Channels.Count; i++) 
     for (var i = 0; i < blocks[0].Channels.Count; i++) 
     { 
      var channelRow = new ChannelRow { Cells = new List<ChannelRowCell>(), Id = i+1 }; 
      foreach (var cc in blocks.Select(block => block.Channels[i])) 
      { 
       channelRow.Cells.Add(new ChannelRowCell 
       { 
        Delay = cc.Delay, 
        Name = cc.Type, 
        Restore = cc.Restore, 
        Trigger = cc.Trigger, 
       }); 
      } 

      channelRows.Add(channelRow); 
     } 

     return channelRows; 
    } 
} 
1

試試這個:

var r = blocks 
    .GroupBy(b => b.Channels.Select(c => c)) 
    .Select(g => new { Channel = g.Key, Blocks = g.Select(b1 => b1) }); 
+0

我不確定這是你想要的 - 它大致類似於'var r = blocks.Select(b => new {Channel = b.Channels.AsEnumerable(),Blocks = Enumerable.Repeat(b,1)} );' – Rawling

2

試着這麼做

var channels = blocks 
    .SelectMany(b => b.Channels.Select(c => new { b, c })) 
    .GroupBy(p => p.c.Id) 
    .Select(g => new { Channel = g.First().c, Blocks = g.Select(p => p.b) }); 

正如其他人所指出的那樣,你Channel類沒有任何地方來存儲Block個序列,所以這將返回匿名對象使用一個名爲ChannelChannel,並呼籲BlocksIEnumerable<Block>的序列。

請注意,它也比較Channel s Id,因爲它們在其他方面似乎不具可比性。如果您的Channel s在Block之間不一致,則只需從Block開始Channel