[英]PredicateBuilder + Join + VB.NET
I have a db that contains 3 tables 我有一个包含3个表的数据库
Products Table Suppliers Table Categories Table Id Id Id ProductName SupplierName CategoryName Quantity . . SupplierId . . CategoryId . .
I am using PredicateBuilder to select Product accoring to the selected product field (Quantity,Productname...) How Can I use PredicateBuilder or any other method to Select the Product according to Its Suppliername or Categoryname 我正在使用PredicateBuilder根据所选产品字段(数量,产品名称...)选择产品,如何使用PredicateBuilder或任何其他方法根据其供应商名称或类别名称选择产品
Please I am using VB.NET I saw many C# examples but I can not understand it nor translate it I am thinking of using join in predicateBuilder but I do not how !!!! 请使用VB.NET,我看到了许多C#示例,但我既不理解也无法翻译,我想在predicateBuilder中使用join,但是我不怎么做!
To be more clear What I want is to combine multiple field in one search ,Like for example: 更清楚地说,我想要在一个搜索中组合多个字段,例如:
Give me a Product where It's Name contains "s" and Quantity <10 and SupplierName is Kimo 给我一个产品,其名称包含“ s”且数量小于10,并且SupplierName为Kimo
Give me a product where i's name contains "g" only 给我一个产品,我的名字只包含“ g”
give me the products for categoryName "Machines" 给我有关“机械”类别名称的产品
. 。
. 。
And this search predicate is changable because Each Products table has many fields ,So the search is dynamic according to selected fields 而且此搜索谓词是可更改的,因为“每个产品”表具有许多字段,因此搜索根据所选字段是动态的
Waiting for your kind help. 等待您的帮助。
I think Arion answer was correct but It needs some revision AnyWay I came up with this solution ,It is not the most effecient one but It solved my problem. 我认为Arion的答案是正确的,但需要进行一些修订。反正我提出了此解决方案,它不是最有效的解决方案,但它解决了我的问题。
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)
. 。 . 。 . 。 Case "Supplier" 案例“供应商”
SupplierAlso = Entry
Case "Category"
CategoryAlso = Ent
Next 下一个
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
For Each it In q With it 对于每个q
DataGridView2.Rows.Add _
({.ID, .ProductName, .UnitPrice, .categoryname, .suppliername, .Location, _
.Description, .SellPrice, .CountInStock, _ .ProductionDate, .ExpiryDate, _
.ProductType, .ProductSeason})
End With
Next
So what do you think ,Is there a better way? 那么您认为呢,有更好的方法吗? Ofcourse yes?Silly question ,But Where? 当然是吗?傻问题,但是在哪里?
So i would do something like this then. 所以我会做这样的事情。 I have simplify the where statements but I think you will get my point: 我简化了where语句,但是我想您会明白我的意思的:
dim checkCategoryName as boolean=true
dim checkSupplier as boolean=true
dim checkQuantity as boolean=true
dim query= db.Products.Select (function(p) p)
if checkCategoryName then
query=query _
.Where (function(p) db.Categories.Where (function(c) c.CategoryName="??" ) _
.Select (function(c) c.Id) _
.Contains(p.CategoryId))
end if
if checkSupplier then
query=query _
.Where (function(q) db.Suppliers.Where (function(s) s.SupplierName="??") _
.Select (function(s) s.Id) _
.Contains(q.SupplierId))
end if
if checkQuantity then
query=query.Where (function(q) q.Quantity<10)
end if
dim result=query.ToList()
Where db is the linq data context. 其中db是linq数据上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.