【发布时间】:2016-01-06 23:03:14
【问题描述】:
因此,我目前在为 Linq 处理 Group By 时遇到了麻烦,本质上我希望从 Pirate 中获取 id 以及 Ships 中的所有信息,理想情况下将其放入 IEnumerable DataRow 列表中(DataRow 被隐藏了......) .
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 获得一个 DataRows 列表,这样如果我遍历每个 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!!!
【问题讨论】:
-
你的问题不是很清楚。什么是输入,什么是预期输出?
-
grab the id from Pirate and all information within Ships, Ideally thrown into a list of IEnumerable.什么? -
您的代码无法编译。
class是保留关键字。这些是私有领域吗?还是应该是公共财产? -
做了一些小的编辑,很抱歉造成混乱,我只是把两个类放在那里作为我正在处理的对象的一个例子。将立即进行更多编辑。
-
DataRow应该包含哪些字段?