我有一個包含3個表PredicateBuilder +加入+ VB.NET
分貝Products Table Suppliers Table Categories Table Id Id Id ProductName SupplierName CategoryName Quantity . . SupplierId . . CategoryId . .
我使用PredicateBuilder選擇產品accoring到所選擇的產品領域(數量,產品名稱......) 我如何據其Suppliername或類別名稱
使用PredicateBuilder或任何其他方法來選擇產品請我使用VB.NET我看到了許多C#的例子,但我不明白,也沒有把它翻譯 我想用加入predicateBuilder的但我不怎麼樣!
爲了更清楚我想要的是多個領域的一個搜索相結合,例如像:
給我看它的名字中包含「s」和數量< 10個產品和SupplierName是奇摩
給我一個我的名字中含有「G」只
給我的產品類別名稱「機」
產品。
。
而這種搜索謂詞是多變的,因爲每一個產品表有很多領域,所以要根據所選字段
等待你的幫助搜索是動態的。
我認爲Arion的答案是正確的,但它需要一些修訂 AnyWay我想出了這個解決方案,它不是最有效的一個,但它解決了我的問題。
Dim SupplierAlso As String = ""
Dim CategoryAlso As String = ""
Dim pred = PredicateBuilder.True(Of Products)()
Select Case Entry
Case "Number"
Dim Inum = Entry
pred = pred.And(Function(m As products) m.ID.Equals(CInt(Inum)))
Case "ProductName"
Dim IName = Entry
pred = pred.And(Function(m As Products) m.ProductName.IndexOf(IName, StringComparison.OrdinalIgnoreCase) >= 0)
。 。 。 案 「供應商」
SupplierAlso = Entry
Case "Category"
CategoryAlso = Ent
下一頁
Dim f As ProductsDataTable = Products.Product
Dim tmp As IEnumerable(Of Products) = New ProductsDataTable().AsEnumerable()
tmp = f.AsEnumerable.Where(pred.Compile)
Dim qry
If CategoryAlso = "" And SupplierAlso = "" Then
q = (From prods In tmp
Join Cats In Categories
On prods.CategoryId Equals Cats.ID
Join Supps In Suppliers
On Supps.ID Equals prods.SupplierId
Select Supps.SupplierName, Cats.CategoryName, prods.ID _
, prods.ProductName, prods.UnitPrice, prods.CategoryId _
, prods.SupplierId, prods.Location, _
prods.Description, prods.SellPrice, prods.CountInStock _
, prods.ProductionDate, prods.ExpiryDate, _
prods.ProductType, prods.ProductSeason).ToList
ElseIf CategoryAlso <> "" And SupplierAlso <> "" Then
q = (From prods In tmp
Join Cats In Categories
On prods.CategoryId Equals Cats.ID
Join Supps In Suppliers
On Supps.ID Equals prods.SupplierId
Where Cats.CategoryName.IndexOf((CategoryAlso)
, StringComparison.OrdinalIgnoreCase) >= 0 And _ Supps.SupplierName.IndexOf((SupplierAlso), _
StringComparison.OrdinalIgnoreCase) >= 0
Select Supps.SupplierName, Cats.CategoryName, prods.ID, _
prods.ProductName, prods.UnitPrice, prods.CategoryId, _
prods.SupplierId, prods.Location, _
prods.Description, prods.SellPrice, prods.CountInStock, _
prods.ProductionDate, prods.ExpiryDate, _
prods.ProductType, prods.ProductSeason).ToList
ElseIf SupplierAlso <> "" And CategoryAlso = "" Then
q = (From prods In tmp
Join Cats In Categories
On Cats.ID Equals prods.CategoryId
Join Supps In Suppliers
On prods.SupplierId Equals Supps.ID Where _
Supps.SupplierName.IndexOf((SupplierAlso), _
StringComparison.OrdinalIgnoreCase) >= 0
Select Cats.CategoryName, Supps.SupplierName, prods.ID, _
prods.ProductName, prods.UnitPrice, prods.CategoryId, _
prods.SupplierId, prods.Location, _
prods.Description, prods.SellPrice, prods.CountInStock, _
prods.ProductionDate, prods.ExpiryDate, _
prods.ProductType, prods.ProductSeason).ToList
ElseIf CategoryAlso <> "" And SupplierAlso = "" Then
q = (From prods In tmp
Join Cats In Categories
On prods.CategoryId Equals Cats.ID Where Cats.CategoryName.IndexOf _
((CategoryAlso), StringComparison.OrdinalIgnoreCase) >= 0
Join Supps In Suppliers On Supps.ID Equals prods.SupplierId
Select Supps.SupplierName, Cats.CategoryName, prods.ID, _
prods.ProductName, prods.UnitPrice, prods.CategoryId, _
prods.SupplierId, prods.Location, _
prods.Description, prods.SellPrice, prods.CountInStock, _
prods.ProductionDate, prods.ExpiryDate, _
prods.ProductType, prods.ProductSeason).ToList
End If
對於每個IT期q 有了它
DataGridView2.Rows.Add _
({.ID, .ProductName, .UnitPrice, .categoryname, .suppliername, .Location, _
.Description, .SellPrice, .CountInStock, _ .ProductionDate, .ExpiryDate, _
.ProductType, .ProductSeason})
End With
Next
那麼你覺得什麼,有沒有更好的辦法?當然是嗎?愚蠢的問題,但在哪裏?
謝謝,但這不是我正在尋找的,Kimo,「s」和<10只是一個虛構的例子,我想用用戶選擇的字段進行動態搜索,可能會使用PredicateBuilder並加入方法,我不知道如何? – 2012-02-06 10:51:10
更新答案 – Arion 2012-02-06 11:51:08
它喜歡你的解決方案,但我不知道爲什麼我無法檢測到CategoryName字段,即使在後期綁定方式中.Item(「CategoryName」)。Tostring – 2012-02-06 14:21:50