所以我现在无法将我的脑袋包裹在Group By目前为Linq,实质上我希望从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!!!
答案 0 :(得分:3)
你可以试试这个:
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);
并像这样使用它:
asnp *share*
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | Out-Null
$ErrorActionPreference = "stop"
$webUrl = "<your Url>"
$listBTitle = "<Title of list B>"
$listATitle = "<Title of list A>"
$listBLookUpColumnName = "<lookup Column name in list b>"
$listAValueToShow = "<Column name of the value that should be displayed in the lookup field>"
$matchColumnA = "<Title of match column list A>"
$matchColumnB = "<Title of match column list B>"
$web = Get-SPWeb $webUrl
$listB = $web.Lists[$listBTitle]
$listA = $web.Lists[$listATitle]
$listB.Items | % {
$lookingItem = $_
$itemToLookIn = $listA.Items | ? { $_[$matchColumnA] -eq $lookingItem[$matchColumnB] }
$lookUpValue = New-Object Microsoft.Sharepoint.SPFieldLookupValue
$lookUpValue.LookupId = $itemToLookIn.ID
$lookUpValue.LookupValue = $itemToLookIn[$listAValueToShow]
$lookingItem[$listBLookUpColumnName] = $lookUpValue
$lookingItem.Update()
}
答案 1 :(得分: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;}
}
}