我正在使用.NET Core 2.0
我有两张桌子:
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评论r ON(m.ID == r.FilmId)WHERE m.AuthorId = 1 AND(r.AuthorID = 3 OR r.SomeText LIKE'%must%');
预期结果应为 CCCC 。
目前我确实有:
List<Movie> movies = _context.Movie.Include(m => m.Reviews.Select(r => r.AuthorId == 3 || r.SomeText.Contains("definitely"))).ToList();
如何使用此表示法表达上述查询?
但是我得到了:
InvalidOperationException:属性表达式'm =&gt; {来自评论 r in m.Reviews select(([r] .AuthorId == __user_ID_0)OrElse [r] .SomeText.Contains(“绝对”))}'无效。表达方式 应代表属性访问权限:'r =&gt; r.MyProperty”。更多 有关包含相关数据的信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=746393
我读过这篇文章,但它什么也没给我。
答案 0 :(得分:3)
我认为这个linq查询返回正确的结果(因为我无法测试)
List<Movie> movies = _context.Movie.where(m => m.AuthorId == 1 && m.Reviews.Any(r =>
r.AuthorId == 3 || r.SomeText.Contains("definitely"))).ToList();