2012-12-02 147 views
1

我是c#的新手,我正在構建一個保存庫(書籍和期刊)內容的應用程序。將字典轉換爲LINQ查詢

我正在構建一個帶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(); 
} 

現在我想要做的是從所有的鍵創建一個查詢&字典中的值的格式爲:

鍵==值。

我該如何以更好的方式做到這一點?我甚至應該使用字典來處理這種類型的行爲?

+1

也許這個鏈接可以幫助:[Dynamic LINQ](http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query -library.aspx) –

+0

謝謝,我結束了與我創建的類傳遞值, ,但謝謝你! :) –

+0

@yuvalos如果你解決了你的問題,請張貼你的決議並接受。 – emd

回答

0

我做的是我最終通過創建我的UI可以看到的數據庫實體的模型(稱爲BookVO) 當用戶將數據插入組合框時,我將該數據插入到BookVO中,那對我的BL,後來傳給我的達爾。然後我使用同一本書搜索我的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; 

這是通過這本書給我的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邏輯我的BL部分:

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; 
}