我正在尝试建立一个过滤系统。假设你有这些模型。
public class FilterVM
{
public string ContentRating { get; set; }
public List<FilterChars> FilterChars { get; set; }
public List<FilterCats> FilterCats { get; set; }
public List<FilterTags> FilterTags { get; set; }
}
public class FilterChars
{
public int CharID { get; set; }
public int CharVal { get; set; }
}
public class Book
{
public int BookID { get; set; }
public ICollection<BookCharacteristic> BookCharacteristic { get; set; }
}
public class BookCharacteristic
{
public int ID { get; set; }
public int BookID { get; set; }
public int CharacteristicID { get; set; }
public Book Book { get; set; }
public int Value { get; set; }
public Characteristic Characteristic { get; set; }
}
因此,一个表单被发布,并且有FilterVM
一个列表,FilterChars
现在我需要找到具有特征 ( CharID
) 并且值大于提交值的书籍。
这是我正在尝试的,但我无法找出编写查询的正确方法。
List<FilterChars> fc = new List<FilterChars>();
foreach (var filter in f.FilterChars.Where(x => x.CharVal > 0)) {
fc.Add(filter);
}
var books = db.Books
.Where(t => fc.Select(y => y.CharID)
.Contains(t.BookCharacteristic
.Any(u => u.CharacteristicID)
)
&& //if there's a match, use the matched BookCharacteristic and Value??
//not sure how to do that
);