2016-01-06 103 views
0

因此,我无法在Group By目前为Linq打包头,本质上我正在寻找从Pirate和船内所有信息中获取ID,理想情况下抛出到IEnumerable DataRow(DataRow隐藏..)的列表中。Linq按列表从列表中进行分组

public class Pirate 
    { 
     public string Id { get; set; } 
     public List<Ship> Ships { get; set; } 
    } 
    public class Ship 
    { 
     public string Name { get; set; } 
     public string ShipClass { get; set; } 
     public string AvastyMast { get; set; } 
    } 

static void Main(string[] args) 
    { 
     List<Pirate> Pirates = new List<Program.Pirate>(); 

     Pirate Arr = new Pirate(); 
     Arr.Id = "1"; 

     Ship Pinta = new Ship(); 
     Pinta.Name = "Maria"; 
     Pinta.ShipClass = "BattleShip"; 
     Pinta.AvastyMast = "You Sunk My BattleShip"; 

     Ship Manta = new Ship(); 
     Pinta.Name = "Clara"; 
     Pinta.ShipClass = "Scout"; 
     Pinta.AvastyMast = "You Sunk My BattleShip!"; 

     Arr.Ships.Add(Pinta); 
     Arr.Ships.Add(Manta); 

     Pirates.Add(Arr); 

     Pirate Sid = new Pirate(); 
     Sid.Id = "2"; 

     Ship Nuclara = new Ship(); 
     Pinta.Name = "Boon"; 
     Pinta.ShipClass = "Sub"; 
     Pinta.AvastyMast = "You Sunk My BattleShip!!"; 

     Ship Nutella = new Ship(); 
     Pinta.Name = "Slimer"; 
     Pinta.ShipClass = "Scout"; 
     Pinta.AvastyMast = "You Sunk My BattleShip!!!"; 


    } 

所以我想要做的就是从上面有通过LINQ的数据行的列表,所以如果我去通过每个DataRow的,并写了每一行和每一列,将产生以下。

1 , Maria, BattleShip, You Sunk My Battleship 
1 , Clara, Scout, You Sunk My Battleship! 
2 , Boon, Sub, You Sunk My Battleship!! 
2, Slimer, Scout, You Sunk My Battleship!!! 
+6

你的问题不是很清楚。什么是输入和什么是预期输出? –

+1

'从海盗和船舶内的所有信息中获取id,理想情况下扔进IEnumerable列表中。“什么? –

+1

您的代码不能编译。 'class'是一个保留关键字。这些私人领域?或者他们应该是公共财产? –

回答

3

你可以试试这个:

var pirates = new List<Pirate>(); // or wherever your pirates will come from 

var data = from p in pirates 
      let pirateId = p.Id 
      from s in p.Ships 
      select new 
      { 
       PirateId = pirateId, 
       Name = s.Name, 
       Class = s.ShipClass, 
       AvastyMast = s.AvastyMast 
      }; 

如果你真的想DataRow对象了吧,你可以创建一个小方法来创建DataRow

private static DataRow CreateDataRow(DataTable table, string pirateId, string name, 
    string shipClass, string avastyMast) 
{ 
    var row = table.NewRow(); 

    // Make sure the column names match those in the table! 
    row["PirateId"] = pirateId; 
    row["Name"] = name; 
    row["ShipClass"] = shipClass; 
    row["AvastyMast"] = avastyMast; 

    return row; 
} 

而且像这样使用它:

var pirates = new List<Pirate>(); // or wherever your pirates will come from 
var dataTable = new DataTable(); // or wherever your DataTable will come from 

var data = from p in pirates 
      let pirateId = p.Id 
      from s in p.Ships 
      select CreateDataRow(dataTable , pirateId, s.Name, s.ShipClass, s.AvastyMast); 
+0

是的,这个假设并不需要数据行。 –

+0

问题在于它来自海盗列表(我最初的问题是格式不正确)。 –

+1

@IvanS为什么这是一个问题?查询将在'pirates'中工作,是'Pirate'对象的集合。 –

1

试试这个

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<Pirate> pirates = new List<Pirate>() { 
        new Pirate() { id = "1" , Ships = new List<Ship>(){ 
         new Ship() { name = "Maria", _class = "BattleShip", avastyemast = "You Sunk My Battleship"}, 
         new Ship() { name = "Clara", _class = "Scout", avastyemast = "You Sunk My Battleship"} 
        } 
       }, 
        new Pirate() { id = "2" , Ships = new List<Ship>() { 
         new Ship() { name = "Boon", _class = "Sub", avastyemast = "You Sunk My Battleship"}, 
         new Ship() { name = "Slimer", _class = "Scout", avastyemast = "You Sunk My Battleship"} 
        } 
       } 
      }; 

      DataTable dt = new DataTable(); 
      dt.Columns.Add("id", typeof(string)); 
      dt.Columns.Add("name", typeof(string)); 
      dt.Columns.Add("class", typeof(string)); 
      dt.Columns.Add("avastyemast", typeof(string)); 

      foreach (Pirate pirate in pirates) 
      { 
       foreach(Ship ship in pirate.Ships) 
       { 
        dt.Rows.Add(new string[] { 
         pirate.id, 
         ship.name, 
         ship._class, 
         ship.avastyemast 
        }); 
       } 

      } 


      //using linq 
      var newRows = pirates.Select(x => x.Ships.Select(y => new List<object>() {x.id, y.name, y._class, y.avastyemast})).SelectMany(z => z).ToList(); 
      foreach (var row in newRows) 
      { 
       dt.Rows.Add(row); 
      } 
     } 
    } 
    public class Pirate 
    { 
     public string id {get;set;} 
     public List<Ship> Ships {get;set;} 

    } 
    public class Ship 
    { 
     public string name {get;set;} 
     public string _class {get;set;} 
     public string avastyemast {get;set;} 
    } 
} 
​ 
+0

这种方式很有效,并且大部分是我认为这样做的原始方式,尽管我觉得使用Linq可能会更高效。谢谢你,我的备份计划的更优化版本。 –

+1

将您的数据添加到示例 – jdweng

+1

添加linq方法 – jdweng