将字典转换为linq查询

时间:2012-12-02 18:38:20

标签: c# linq dictionary

我是c#的新手,我正在构建一个包含图书馆(书籍和期刊)内容的应用程序。

我正在使用UI,BLL和DAL构建一个3层程序。

我要做的是从组合框中获取文本并将其(最终)在我的DAL中转换为Linq to SQL查询。 我创建了一个本地数据库并将其映射到实体框架。

我有以下代码:

BL.MainLogic BLL = new MainLogic();
Dictionary<string, string> searchList = new Dictionary<string, string>();
searchList.Add("ItemID", cmboBxItemID.Text);
searchList.Add("ItemName", cmboBxItemName.Text);
searchList.Add("ItemType", cmboBxItemType.Text);
searchList.Add("Author", cmboBxAuthor.Text);
searchList.Add("Publisher", cmboBxPublisher.Text);
searchList.Add("Price", cmboBxPrice.Text);
DataTable searchResult = BLL.Search(searchList);
searchGridView.DataSource = searchList;

我正在创建一个包含列名和要搜索的值的字典,然后将其传递给BL:

public DataTable Search(Dictionary<string,string> SearchParams)
{
    var itemType = SearchParams.FirstOrDefault(x => x.Key == "ItemType");
    IQueryable query = SearchParams.AsQueryable();
}

现在我要做的是创建一个来自所有Keys&amp;的查询。字典中的值格式为:

Key == Value。

我怎样才能以更好的方式做到这一点?我是否应该使用字典进行此类操作?

1 个答案:

答案 0 :(得分:0)

我做的是我最终通过创建我的UI可以看到的数据库实体的模拟(称为BookVO) 当用户将数据插入组合框时,我将该数据插入到BookVO中,然后将其传递给我的BL,然后将其传递给我的dal。然后我用同一本书搜索我的EF背景。

这是我添加这本书的地方:

private BookVO AddNewBook()
{
    BookVO bookToAdd = new BookVO()
    {
        Author = txtBxBookAuthor.Text,
        Discount = double.Parse(txtBxBookDiscount.Text),
        Genre = txtBxBookGenre.Text,
        ISBN = txtBxBookISBN.Text,
        Name = txtBxBookName.Text,
        Price = double.Parse(txtBxBookPrice.Text),
        Publisher = txtBxBookPublisher.Text,
        ReleaseDate = DateTime.Parse(dateTimeBookPicker.Text)
    };
    return bookToAdd;

这是我的BL部分,将书传递给我的DAL

public List<BookVO> SearchBook(BookVO book)
{
    List<Books> queryResult = BLDAL.SelectBookFromDB(book);
    List<BookVO> returnedBook = new List<BookVO>();
    foreach (Books dBBook in queryResult)
    {
        BookVO thisBook = new BookVO() {Author = dBBook.Author, Name = dBBook.Name, ISBN = dBBook.BookISBN, Discount = dBBook.Discount, Price = dBBook.Price, Publisher = dBBook.Publisher, ReleaseDate = dBBook.ReleaseDate};
        returnedBook.Add(thisBook);
    };
    return returnedBook;
}

这是我的搜索DAL逻辑:

public List<Books> SelectBookFromDB(BookVO book)
{
    var result = myEntities.Books.ToList();
    if (string.IsNullOrWhiteSpace(book.Author) == false)
    {
        result = result.Where(x => x.Author == book.Author).ToList();
    }
    if (string.IsNullOrWhiteSpace(book.Name) == false)
    {
        result = result.Where(x => x.Name == book.Name).ToList();
    }
    if (string.IsNullOrWhiteSpace(book.Publisher) == false)
    {
        result = result.Where(x => x.Publisher == book.Publisher).ToList();
    }
    if (string.IsNullOrWhiteSpace(book.ISBN) == false)
    {
        result = result.Where(x => x.BookISBN == book.ISBN).ToList();
    }
    if (book.Price != 0)
    {
        result = result.Where(x => x.Price == book.Price).ToList();
    }
    if (book.Discount != 0)
    {
        result = result.Where(x => x.Discount == book.Discount).ToList();
    }

    return result;
}