我目前有这个linq声明..
Dim results = (From pb In db.ParcelBases
Join l In db.LegalDescs On pb.lrsn Equals l.LRSN
Join sh In db.SalesHists On pb.lrsn Equals sh.lrsn
Where sh.sale_date >= dateFrom
Where sh.sale_date <= dateTo
Where sh.sale_amt >= psfldLow
Where sh.sale_amt <= psfldHigh
Select New With {
pb.lrsn,
pb.prop_street,
pb.owner1,
l.LEG_DESCRIPTION,
pb.parcel_id
}).ToList
但是,我还需要搜索大约7个可选字段。做这个的最好方式是什么?我是否必须检查这些字段是否为空并为每个检查创建一个语句?或者有没有办法在声明中做到这一点?声明中当前的那些是最小选择所需的必填字段。
更新:我试图完成这样的事情..
Dim results = (From pb In db.ParcelBases
Join l In db.LegalDescs On pb.lrsn Equals l.LRSN
Join sh In db.SalesHists On pb.lrsn Equals sh.lrsn
Where sh.sale_date >= dateFrom 'required field
Where sh.sale_date <= dateTo 'required field
Where sh.sale_amt >= psfldLow 'required field
Where sh.sale_amt <= psfldHigh 'required field
Where pb.propcity = tbPropCity 'optional field-could be null
Where pb.proptype = tbPropType 'optional field-could be null
Select New With {
pb.lrsn,
pb.prop_street,
pb.owner1,
l.LEG_DESCRIPTION,
pb.parcel_id
}).ToList
答案 0 :(得分:0)
您的意思是Or
声明,例如:
Dim results = (From pb In db.ParcelBases
Join l In db.LegalDescs On pb.lrsn Equals l.LRSN
Join sh In db.SalesHists On pb.lrsn Equals sh.lrsn
Where (sh.sale_date >= dateFrom
And (sh.sale_date <= dateTo)
And (sh.sale_amt >= psfldLow)
And (sh.sale_amt <= psfldHigh)
Or (tbPropCity <> Nothing and pb.propcity = tbPropCity) ' check for nulls here if necessary or something else Or... etc. )
Select New With {
pb.lrsn,
pb.prop_street,
pb.owner1,
l.LEG_DESCRIPTION,
pb.parcel_id
}).ToList
至少它听起来像你在寻找
答案 1 :(得分:0)
您可以使用可选字段制作视图模型。
public class Thing {
public type lrsn {get;set;}
public type prop_street {get;set;}
public type owner1 {get;set;}
public type LEG_DESCRIPTION {get;set;}
public type? propcity {get;set;}
public type? proptype {get;set;}
public Thing(
type lrsn,
type prop_street,
type owner1,
type LEG_DESCRIPTION,
type? propcity = null,
type? proptype = null)
{
this.lrsn = lrsn;
this.prop_street = prop_street;
this.owner1 = owner1;
this.LEG_DESCRIPTION;
this.propcity = propcity;
this.proptype = proptype;
}
}
然后你的代码在哪里:
using namespace.Thing;
Dim results = (From pb In db.ParcelBases
Join l In db.LegalDescs On pb.lrsn Equals l.LRSN
Join sh In db.SalesHists On pb.lrsn Equals sh.lrsn
Where sh.sale_date >= dateFrom
Where sh.sale_date <= dateTo
Where sh.sale_amt >= psfldLow
Where sh.sale_amt <= psfldHigh
Select New Thing(
pb.lrsn,
pb.prop_street,
pb.owner1,
l.LEG_DESCRIPTION,
pb.parcel_id
)).ToList