C# 如何访问作者中的值?
嗯,我必须列出作者及其著作和评论的数量,我必须在LINQ的帮助下以XML格式编写 以下是数据(只有书籍、作者和评论的部分才是重要的) 这是我写的(我只能打印作者) 它应该是这样的: [1] : 一般来说,我的问题是,我实际上只能打印作者(如果我使用C# 如何访问作者中的值?,c#,xml,linq,C#,Xml,Linq,嗯,我必须列出作者及其著作和评论的数量,我必须在LINQ的帮助下以XML格式编写 以下是数据(只有书籍、作者和评论的部分才是重要的) 这是我写的(我只能打印作者) 它应该是这样的: [1] : 一般来说,我的问题是,我实际上只能打印作者(如果我使用SampleData.authors),但当我想使用SampleData.Books时,我不知道如何访问作者中的值(它所代表的部分“new[]{authors[]}让我感到困惑)。尝试以下内容: using System; using System.C
SampleData.authors
),但当我想使用SampleData.Books
时,我不知道如何访问作者中的值(它所代表的部分“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] };
}
}
}
你的数据库布局真不寻常
在用这个创建严肃软件之前,考虑创建主键和外键,并实现适当的一对多表关系和多对多表关系。
如果你不知道我在说什么,请你的项目负责人花点时间研究一下基本的数据库概念。将来会有回报的 所以你有一系列的书籍
,每本书籍
都有一个或多个作者
,每本书都没有更多的评论。作者已经在你的书里了,但是你决定把评论放在一个单独的表格里
我必须列出作者及其著作和评论的数量
还没有写过书的作家还是作家吗?或者你只想要写过至少一本书的作者?您的图书列表中是否有作者不在您的作者列表中?您的作者列表中的作者是否唯一,或者您是否有几个不同的作者具有相同的名字和姓氏
要把原来的“有作者的书”变成“有作者的书”,并不难
我不确定“他们的评论数量”。你是说这些作者对他们所有的书都有多少评论,还是每本书都有多少评论?你只想要评论的数量,而不是评级吗
通常情况下,如果项目中有零个或多个子项目,如教师和学生,并且您希望将其反转,那么学生和他们的教师,您可以使用SelectMany将教师和学生的组合展平为一个教师和一个学生,然后按学生分组,以获得学生和他们的教师
因此,首先我们将“书籍及其作者”扁平化为[书籍,作者]组合,然后按作者分组:
var result = books.SelectMany(book => book.Author)
输入:
- 与作者1、2合著的书A
- B书与作者3
- C书作者1、3
.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(),
})
换句话说:从作者表中的每一位作者那里,得到他的名字和姓氏。获取至少有一位作者与此作者相等的所有书籍,并从这些书籍中选择多个属性
现在,要获得每本书的评论数量,请输入“选择我要写的地方”:
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。谢谢您的回答
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(),