[英]How can I access the values in Authors?
好吧,我必須列出作者的書和他們的評論數量,我必須在 LINQ 的幫助下以 XML 格式寫出來。
這是數據(只有書籍、作者和評論所在的部分很重要)
static public class SampleData
{
static public Publisher[] Publishers =
{
new Publisher {Name="FunBooks"},
new Publisher {Name="Joe Publishing"},
new Publisher {Name="I Publisher"}
};
static public Author[] Authors =
{
new Author {FirstName="Johnny", LastName="Good"},
new Author {FirstName="Graziella", LastName="Simplegame"},
new Author {FirstName="Octavio", LastName="Prince"},
new Author {FirstName="Jeremy", LastName="Legrand"}
};
static public Subject[] Subjects =
{
new Subject {Name="Software development",Description="developing and others"},
new Subject {Name="Novel",Description="great feelings"},
new Subject {Name="Science fiction",Description="out of space and everywhere"}
};
static public Book[] Books =
{
new Book { // [0]
Title="Funny Stories",
Publisher=Publishers[0],
Authors=new[]{Authors[0], Authors[1]},
PageCount=101,
Price=25.55M,
PublicationDate=new DateTime(2004, 11, 10),
Isbn="0-000-77777-2",
Subject=Subjects[0]
},
new Book { // [1]
Title="LINQ rules",
Publisher=Publishers[1],
Authors=new[]{Authors[2]},
PageCount=300,
Price=12M,
PublicationDate=new DateTime(2007, 9, 2),
Isbn="0-111-77777-2",
Subject=Subjects[0]
},
new Book { // [2]
Title="C# on Rails",
Publisher=Publishers[1],
Authors=new[]{Authors[2]},
PageCount=256,
Price=35.5M,
PublicationDate=new DateTime(2007, 4, 1),
Isbn="0-222-77777-2",
Subject=Subjects[0]
},
new Book { // [3]
Title="All your base are belong to us",
Publisher=Publishers[1],
Authors=new[]{Authors[3]},
PageCount=1205,
Price=35.5M,
PublicationDate=new DateTime(2006, 5, 5),
Isbn="0-333-77777-2",
Subject=Subjects[2]
},
new Book { // [4]
Title="Bonjour mon Amour",
Publisher=Publishers[0],
Authors=new[]{Authors[1], Authors[2]},
PageCount=50,
Price=29M,
PublicationDate=new DateTime(1973, 2, 18),
Isbn="2-444-77777-2",
Subject=Subjects[1]
}
};
static public User[] Users =
{
new User{Name="Fred"},
new User{Name="Barney"},
new User{Name="Wilma"}
};
static public Review[] Reviews =
{
new Review{ Book = Books[0], Comments="cc1", Rating=2, User=Users[0]},
new Review{ Book = Books[0], Comments="cc2", Rating=3, User=Users[2]},
new Review{ Book = Books[1], Comments="cc3", Rating=1, User=Users[0]},
new Review{ Book = Books[1], Comments="cc4", Rating=2, User=Users[1]},
new Review{ Book = Books[1], Comments="cc5", Rating=1, User=Users[2]},
new Review{ Book = Books[2], Comments="cc6", Rating=3, User=Users[2]},
new Review{ Book = Books[3], Comments="cc7", Rating=4, User=Users[2]},
new Review{ Book = Books[4], Comments="cc8", Rating=5, User=Users[1]}
};
static SampleData()
{
// Books -- Reviews haben einnen Doppelverweise
// daher kann erst hier Book-->Review gesetzt werden
Books[0].Reviews = new[] { Reviews[0], Reviews[1] };
Books[1].Reviews = new[] { Reviews[2], Reviews[3], Reviews[4] };
Books[2].Reviews = new[] { Reviews[5] };
Books[3].Reviews = new[] { Reviews[6] };
Books[4].Reviews = new[] { Reviews[7] };
}
}
這是我寫的(我只能打印作者)
var elements2 = new XElement("Autoren", SampleData.Authors
.GroupBy(g => (g.FirstName + " " + g.LastName))
.Select(g => new XElement("Autor", new XAttribute("Name", g.Key)))
);
var xml2 = new XElement("AuthorReviews", elements2);
Console.WriteLine(xml2);
它應該看起來像這樣:[1]: https://i.stack.imgur.com/pQS2q.png
一般來說,我的問題是,我實際上只能打印作者(如果我使用SampleData.Authors
),但是當我想使用SampleData.Books
時,我不知道如何訪問 Authors 中的值(它所在的部分“new[]{Authors[..]}”讓我很困惑)。
嘗試以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication176
{
class Program
{
static void Main(string[] args)
{
XElement autoren = new XElement("Autoren");
foreach (Author author in SampleData.Authors)
{
string fullName = author.FirstName + " " + author.LastName;
XElement autor = new XElement("Author", new XAttribute("Name", fullName));
autoren.Add(autor);
foreach(Book book in SampleData.Books.Where(x => x.Authors.Any(y => y.FirstName + " " + y.LastName == fullName)))
{
XElement xBook = new XElement("Buch", new object[] {new XAttribute("Title", book.Title), new XAttribute("Anzreviews", book.Reviews.Average(x => x.Rating))});
autor.Add(xBook);
}
}
}
}
public class Publisher
{
public string Name { get; set; }
}
public class Author
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Subject
{
public string Name { get; set; }
public string Description { get; set; }
}
public class Book
{
public string Title { get; set; }
public Publisher Publisher { get; set; }
public Author[] Authors { get; set; }
public int PageCount { get; set; }
public decimal Price { get; set; }
public DateTime PublicationDate { get; set; }
public string Isbn { get; set; }
public Subject Subject { get; set; }
public Review[] Reviews { get; set; }
}
public class User
{
public string Name { get; set; }
}
public class Review
{
public Book Book { get;set;}
public string Comments {get;set;}
public int Rating { get;set;}
public User User { get;set;}
}
static public class SampleData
{
static public Publisher[] Publishers =
{
new Publisher {Name="FunBooks"},
new Publisher {Name="Joe Publishing"},
new Publisher {Name="I Publisher"}
};
static public Author[] Authors =
{
new Author {FirstName="Johnny", LastName="Good"},
new Author {FirstName="Graziella", LastName="Simplegame"},
new Author {FirstName="Octavio", LastName="Prince"},
new Author {FirstName="Jeremy", LastName="Legrand"}
};
static public Subject[] Subjects =
{
new Subject {Name="Software development",Description="developing and others"},
new Subject {Name="Novel",Description="great feelings"},
new Subject {Name="Science fiction",Description="out of space and everywhere"}
};
static public Book[] Books =
{
new Book { // [0]
Title="Funny Stories",
Publisher=Publishers[0],
Authors=new[]{Authors[0], Authors[1]},
PageCount=101,
Price=25.55M,
PublicationDate=new DateTime(2004, 11, 10),
Isbn="0-000-77777-2",
Subject=Subjects[0]
},
new Book { // [1]
Title="LINQ rules",
Publisher=Publishers[1],
Authors=new[]{Authors[2]},
PageCount=300,
Price=12M,
PublicationDate=new DateTime(2007, 9, 2),
Isbn="0-111-77777-2",
Subject=Subjects[0]
},
new Book { // [2]
Title="C# on Rails",
Publisher=Publishers[1],
Authors=new[]{Authors[2]},
PageCount=256,
Price=35.5M,
PublicationDate=new DateTime(2007, 4, 1),
Isbn="0-222-77777-2",
Subject=Subjects[0]
},
new Book { // [3]
Title="All your base are belong to us",
Publisher=Publishers[1],
Authors=new[]{Authors[3]},
PageCount=1205,
Price=35.5M,
PublicationDate=new DateTime(2006, 5, 5),
Isbn="0-333-77777-2",
Subject=Subjects[2]
},
new Book { // [4]
Title="Bonjour mon Amour",
Publisher=Publishers[0],
Authors=new[]{Authors[1], Authors[2]},
PageCount=50,
Price=29M,
PublicationDate=new DateTime(1973, 2, 18),
Isbn="2-444-77777-2",
Subject=Subjects[1]
}
};
static public User[] Users =
{
new User{Name="Fred"},
new User{Name="Barney"},
new User{Name="Wilma"}
};
static public Review[] Reviews =
{
new Review{ Book = Books[0], Comments="cc1", Rating=2, User=Users[0]},
new Review{ Book = Books[0], Comments="cc2", Rating=3, User=Users[2]},
new Review{ Book = Books[1], Comments="cc3", Rating=1, User=Users[0]},
new Review{ Book = Books[1], Comments="cc4", Rating=2, User=Users[1]},
new Review{ Book = Books[1], Comments="cc5", Rating=1, User=Users[2]},
new Review{ Book = Books[2], Comments="cc6", Rating=3, User=Users[2]},
new Review{ Book = Books[3], Comments="cc7", Rating=4, User=Users[2]},
new Review{ Book = Books[4], Comments="cc8", Rating=5, User=Users[1]}
};
static SampleData()
{
// Books -- Reviews haben einnen Doppelverweise
// daher kann erst hier Book-->Review gesetzt werden
Books[0].Reviews = new[] { Reviews[0], Reviews[1] };
Books[1].Reviews = new[] { Reviews[2], Reviews[3], Reviews[4] };
Books[2].Reviews = new[] { Reviews[5] };
Books[3].Reviews = new[] { Reviews[6] };
Books[4].Reviews = new[] { Reviews[7] };
}
}
}
你有多么不尋常的數據庫布局!
在你用這個創建嚴肅的軟件之前,考慮創建主鍵和外鍵,並實現正確的一對多表關系和多對多表關系。
如果您不知道我在說什么,請向您的項目負責人請教一些基本的數據庫想法。 它會在未來得到回報。
因此,您有一系列Books
,每Book
都有一個或多個Authors
,並且每本書都有零多個評論。 作者已經在您的圖書中,但您決定將評論放在單獨的表格中
我必須列出作者及其書籍和評論數量
還沒寫過書的作者還是作者嗎? 還是您只想要至少寫過一本書的作者? 您的書籍列表中是否有作者不在您的作者列表中? 您的作者列表中的作者是唯一的,還是您有幾個不同的作者具有相同的名字和姓氏?
將原來的“書有作者”變成“作者有書”並不難
我不確定“他們的評論數量”。 你是指這些作者對他們所有書籍的評論數量,還是他們每本書的評論數量? 你只想要評論的數量,而不是評分嗎?
通常,如果您有帶有零個或多個子項的項目,例如教師和學生,並且您想要反轉這一點,因此學生和他們的教師,您使用 SelectMany 將教師和學生扁平化為一名教師和一名學生的組合,然后 GroupBy Student 讓學生和他們的老師在一起。
所以首先我們將“Books with their Authors”扁平化為 [Book, Author] 組合,然后我們 groupby Author:
var result = books.SelectMany(book => book.Author)
輸入:
在 SelectMany 之后:[A, 1], [A, 2], [B, 3], [C,1], [C, 3]
繼續 LINQ:GroupBy 作者:
.GroupBy(authorBookCombination => authorBookCombination.Author,
// parameter resultSelector: take every Author, with all his Books, to create one new
(author, booksOfThisAuthor) => new
{
// Select only the Author properties that you want to save
FirstName = author.FirstName,
LastName = author.LastName,
Books = booksOfThisAuthor.Select(book => new
{
// select only the Book properties that you want to save:
Title = book.Title,
Comments = book.Comments,
... // etc
// don't Add the Authors!
// TODO: "amount" of Reviews
}
這是最簡單的部分:讓作者帶着他們的書。 如果您還想要還沒有寫任何東西的作者,請從作者表開始,為每個作者找到他的零個或多個書:
var result = Authors.Select(author => new
{
FirstName = author.FirstName,
LastName = author.LastName,
Books = Books.Where(book => book.Author.Any(bookAuthor => bootkAuthor == author))
.Select(book => new
{
Title = book.Title,
...
// TODO reviews
})
.ToList(),
})
用文字表示:從 Authors 表中的每個 Author 中,獲取他的名字和姓氏。 從這些書中獲取至少有一個作者等於該作者的所有書籍,以及 select 幾個屬性。
現在要獲取每本書的評論數量,請輸入我寫 TODO 的 Select:
ReviewAmount = reviews.Where(review => review.Book == book).Count(),
簡而言之:對於我們為這位作者拍攝的每一本書,請查看表格或評論。 每一篇評論都是關於一本書。 只保留與我們正在創建的書相同的書的那些評論。
如果您想要的不僅僅是數量或評論:
ReviewInformation = reviews.Where(review => review.Book == book)
.Select(review => new
{
Rating = review.Rating,
Comments = review.Comments,
})
.ToList(),
請注意,ReviewInformation.Count 是您最初要求的評論數量。
結果是一系列作者,每個作者都有他的零或多本書,每本書都有(數量)其零或更多評論。
現在您已經有了這個,您可以使用 XMLSerializer 將其序列化為 XML。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.