我有一個帶有串列的模型,有什么方法可以過濾該串列嗎?
我正在嘗試的是:
List<Booking> a = await _context.Set<Booking>().Include(u => u.BookingLine).Include(u => u.BookingStatus).ToListAsync();
在該 Booking 模型中,有一個 List。我只想在該 BookingStatus 中獲取所有 StatusId = 1。
Booking 模型和 BookingStatus 模型具有相同的 BookingId。我不知道在哪里將 Where() 放在該 linq 中。
我試過這樣但它回傳了一個錯誤:
_context.Set<Booking>().Include(u => u.BookingLine).Include(u => u.BookingStatus
.LastOrDefault(a => a.StatusId == 1 || a.StatusId == 7 || a.StatusId == 13)).ToListAsync();
錯誤:
System.InvalidOperationException: The expression 'u.BookingStatus.AsQueryable().LastOrDefault(a => (((a.StatusId == 1) OrElse (a.StatusId == 7)) OrElse (a.StatusId == 13)))' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data
uj5u.com熱心網友回復:
我認為您的意思是使用“Where”而不是“LastOrDefault”。
就像是:
_context.Set<Booking>().Include(u => u.BookingLine).Include(u => u.BookingStatus
.Where(a => a.StatusId == 1 || a.StatusId == 7 || a.StatusId == 13)).ToListAsync();
急切加載的 Microsoft 示例:
using (var context = new BloggingContext())
{
var filteredBlogs = context.Blogs
.Include(
blog => blog.Posts
.Where(post => post.BlogId == 1)
.OrderByDescending(post => post.Title)
.Take(5))
.ToList();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/463212.html
上一篇:EFCore選擇有條件的物件
