首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按列表进行分组并从这两个列表中进行选择

按列表进行分组并从这两个列表中进行选择
EN

Stack Overflow用户
提问于 2016-01-06 13:46:24
回答 2查看 89关注 0票数 0

因此,我很难把我的头绕在集团周围,目前我正在为Linq服务,本质上我想从海盗和船上的所有信息中获取id,最好是把它扔到IEnumerable DataRow列表中(DataRow被隐藏了.)。

代码语言:javascript
复制
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并写出每一行和每一列,它就会产生以下内容。

代码语言:javascript
复制
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!!!
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-06 14:00:15

你可以试试这个:

代码语言:javascript
复制
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的小方法。

代码语言:javascript
复制
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;
}

像这样使用它:

代码语言:javascript
复制
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);
票数 3
EN

Stack Overflow用户

发布于 2016-01-06 14:03:37

尝尝这个

代码语言:javascript
复制
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;}
    }
}
​
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34634671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档