C# 我如何与linq建立多对多关系
我想问我如何才能查询每本书的类型,我尝试了很多方法,但都失败了 书籍: 使用SelectMany:C# 我如何与linq建立多对多关系,c#,linq,concatenation,inner-join,C#,Linq,Concatenation,Inner Join,我想问我如何才能查询每本书的类型,我尝试了很多方法,但都失败了 书籍: 使用SelectMany: class Program { static void Main(string[] args) { Context db = new Context(); var results = db.Book.SelectMany(x => x.Genres.Select(y => new
class Program
{
static void Main(string[] args)
{
Context db = new Context();
var results = db.Book.SelectMany(x => x.Genres.Select(y => new
{
id = x.Id,
title = x.Title,
authorId = x.Author.Id,
authorName = x.Author.FullName,
genreId = y.Id,
genreName = y.Name
})).ToList();
}
}
public class Context
{
public List<Book> Book { get; set; }
public List<Genre> Genre { get; set; }
public List<Author> Author { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
public virtual Author Author { get; set; }
}
public class Genre
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Author
{
public int Id { get; set; }
public string FullName { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
Context db=newcontext();
var results=db.Book.SelectMany(x=>x.Genres.Select(y=>new
{
id=x.id,
title=x.title,
authorId=x.Author.Id,
authorName=x.Author.FullName,
genreId=y.Id,
genreName=y.Name
})).ToList();
}
}
公共类上下文
{
公共列表簿{get;set;}
公共列表类型{get;set;}
公共列表作者{get;set;}
}
公共课堂用书
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共虚拟ICollection类型{get;set;}
公共虚拟作者{get;set;}
}
公共课体裁
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection图书{get;set;}
}
公共类作者
{
公共int Id{get;set;}
公共字符串全名{get;set;}
公共虚拟ICollection图书{get;set;}
}
public class Genre
{
public int Id { get; set; }
[StringLength(32)]
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Author
{
public int Id { get; set; }
public string FullName { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
from book in Books
join author in Authors on book.Author.Id equals author.Id
??join Genre ??
select new {book, author, ?list genres}
class Program
{
static void Main(string[] args)
{
Context db = new Context();
var results = db.Book.SelectMany(x => x.Genres.Select(y => new
{
id = x.Id,
title = x.Title,
authorId = x.Author.Id,
authorName = x.Author.FullName,
genreId = y.Id,
genreName = y.Name
})).ToList();
}
}
public class Context
{
public List<Book> Book { get; set; }
public List<Genre> Genre { get; set; }
public List<Author> Author { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
public virtual Author Author { get; set; }
}
public class Genre
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Author
{
public int Id { get; set; }
public string FullName { get; set; }
public virtual ICollection<Book> Books { get; set; }
}