我正在使用 .NET Core 2.0
我有2张桌子:
Movies
ID | Name | AuthorId
----------------------
1 | AAAA | 1
2 | BBBB | 1
3 | CCCC | 1
4 | DDDD | 2
Reviews
ID | FilmId | AuthorId | SomeText
------------------------------------
1 | 1 | 1 | this is horror
2 | 1 | 2 | this is comendy
3 | 3 | 1 | it may be horror
4 | 3 | 2 | it is definitely comedy, not horror
我的模型是:
class Movie {
[Key]
public int ID { get; set; }
public string Name { get; set; }
public int AuthorId { get; set; }
public List<Review> Reviews { get; set; }
}
class Review {
[Key]
public int ID { get; set; }
public string SomeText { get; set; }
public int AuthorId { get; set; }
public int FilmId { get; set; }
public Movie Film { get; set; }
}
我的上下文是:
public class MyContext : DbContext
{
public DbSet<Movie> Movie { get; set; }
public DbSet<Review> Review { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Movie>().HasMany(m => m.Reviews).WithOne(r => r.Film);
}
}
现在我想做这样的查询:
SELECT * FROM Movies m JOIN Reviews r ON (m.ID == r.FilmId) WHERE m.AuthorId = 1 AND (r.AuthorID = 3 OR r.SomeText LIKE '%definely%');
预期的结果应该是CCCC。
目前我有:
List<Movie> movies = _context.Movie.Include(m => m.Reviews.Select(r => r.AuthorId == 3 || r.SomeText.Contains("definitely"))).ToList();
如何用这种表示法表达上述查询?
但是我确实得到:
InvalidOperationException: 属性表达式 'm => {from Review r in m.Reviews select (([r].AuthorId == __user_ID_0) OrElse [r].SomeText.Contains("肯定"))}' 无效。该表达式应表示属性访问:'r => r.MyProperty'。有关包含相关数据的详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=746393。
我读过这篇文章,但它什么也没给我。
相关分类