我有这张表和关系。
修改
类
public class Person
{
public Person()
{
Books = new HashSet<Book>();
}
public int PersonId { get; set; }
public string fname { get; set; }
public string lname { get; set; }
public string Company { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Book
{
public Book()
{
Persons = new HashSet<Person>();
}
public int BookId { get; set; }
public string Title { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
班级数据
{
lname = "Bar", fname = "Foo", Company = "FooBar", Books = { Title = "Book1" }, { Title = "Book2" }
}
人员表
+-------+-------+---------+----------+
| fname | lname | Company | PersonId |
+-------+-------+---------+----------+
| Foo | Bar | FooBar | 1 |
| Bob | Joe | Forest | 2 |
| Temp | Yoo | Temp | 3 |
+-------+-------+---------+----------+
书目表
+--------+-------+
| BookId | Title |
+--------+-------+
| 1 | Book1 |
| 2 | Book2 |
| 3 | Book3 |
+--------+-------+
PersonBook Table
+----------+--------+
| PersonId | BookId |
+----------+--------+
| 1 | 2 |
| 1 | 1 |
| 2 | 1 |
+----------+--------+
结果
+----------+---------+------+
| Name | Company | Book |
+----------+--------+-------+
| Yoo, Temp| Temp | |
| Joe, Bob | Forest | Book1 |
| Bar, Foo | FooBar | Book1 |
| Bar, Foo | FooBar | Book2 |
+----------+--------+-------+
现在基本上,Person和Book有很多对多的关系。我已经有了一个Sql Query但是如何在Linq查询版本中执行此操作?
嗯,这样的事情:
select
(isnull(c.lname, '') + ', ' + isnull(c.fname, '')) as Name
,isnull(c.Company , '') as Company
,isnull(f.Book, '') as Book
from Person c
left join PersonBook cf
on c.PersonId = cf.PersonId
left join Book f
on f.BookId = cf.BookId
order by f.Title, c.lname
任何帮助都会有很大的帮助!谢谢!
答案 0 :(得分:2)
LINQ中的左连接有点棘手,需要join into
和DefaultIfEmpty
:
from p in db.People
join bj in db.Books on p.PersonId equals bj.PersonID into j
from b in j.DefaultIfEmpty()
select new { p.fName, p.lName, p.Company, b == null ? null : b.Title }
答案 1 :(得分:1)
这就是我所拥有的: -
var result = from x in (from p in persons
join pb in personBooks
on p.PersonID equals pb.PersonID into g
from result1 in g.DefaultIfEmpty()
select new
{
Name = p.lName + "," + p.fName,
Company = p.Company,
BookID = result1 == null ? 0 : result1.BookID
})
join y in books
on x.BookID equals y.BookID into g2
from result2 in g2.DefaultIfEmpty()
select new
{
Name = x.Name,
Company = x.Company,
Book = result2 == null ? String.Empty : result2.Title
};
我使用了一些凌乱的范围变量名,请在实际查询中更改它们:)
获得以下结果: -
答案 2 :(得分:1)
查看您的课程,您的模型中不应该是PersonBook
课程。使用EF,您可以在没有联结类的情况下为多个关联建模。 EF将透明地查询数据库中的联结表。
所以你的查询应该是
var query = from p in db.People
from b in p.Books.DefaultIfEmpty() // For a left outer join
select new { p.fName, p.lName, p.Company, b.Title };
var result = query.asEnumerable()
.Select(x => new
{
Name = string.Format("{0}, {1}", x.lName, x.fName),
p.Company,
Book = b.Title
}
如果您监视生成的SQL,您将看到EF加入PersonBook
表。
答案 3 :(得分:0)
使用Linq方法,您可以这样使用SelectMany:
var PersonBook = Persons
.SelectMany(p => p.Books,
(p, b) =>
new
{
Name = p.fname + ", " + p.lname,
Company = p.Company,
BookId = b == null ? -1 : b.BookId,
})
.Join(Books, p => p.BookId, b => b.BookId,
(p, b) =>
new
{
Name = p.Name,
Company = p.Company,
Book = p.BookId == -1 ? "" : b.Title
});