1

我正在尝试建立一个过滤系统。假设你有这些模型。

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
 );
4

1 回答 1

1
    public class BookCharacteristicEqualityComparer : IEqualityComparer<BookCharacteristic>
    {
        public bool Equals(BookCharacteristic x, BookCharacteristic y)
        {
            return x.CharacteristicID == y.CharacteristicID && x.Value == y.Value;
        }

        public int GetHashCode(BookCharacteristic obj)
        {
            return obj.CharacteristicID * obj.Value;
        }
    }

这是为了将 BookCharacteristics 与 id 和 value 进行比较

    var books = db.Books
        .Where((book) =>
        {
            foreach (var filterChar in fc)
            {
                if (!book.BookCharacteristic.Contains(new BookCharacteristic() {CharacteristicID = filterChar.CharID, Value = filterChar.CharVal},
                                                        new BookCharacteristicEqualityComparer()))
                    return false;
            }

            return true;
        });

从过滤器中找到包含所有特征和值的所有书籍

希望有帮助:)

编辑:这是我的代码,编译并运行良好(我没有结果,因为我没有数据,但没有错误)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        var fc = new List<FilterChars>();
        var dbBooks = new List<Book>();

        var books = dbBooks
        .Where((book) =>
        {
        foreach (var filterChar in fc)
        {
            if (!book.BookCharacteristic.Contains(new BookCharacteristic() { CharacteristicID = filterChar.CharID, Value = filterChar.CharVal },
                                                    new BookCharacteristicEqualityComparer()))
                return false;
        }

        return true;
        });
    }


}

public class FilterVM
{
    public string ContentRating { get; set; }
    public List<FilterChars> FilterChars { 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 class BookCharacteristicEqualityComparer : IEqualityComparer<BookCharacteristic>
{
    public bool Equals(BookCharacteristic x, BookCharacteristic y)
    {
        return x.CharacteristicID == y.CharacteristicID && x.Value == y.Value;
    }

    public int GetHashCode(BookCharacteristic obj)
    {
        return obj.CharacteristicID * obj.Value;
    }
}
于 2013-04-17T06:14:16.747 回答