读书人

帮看看这个Linq,如何重复使用查询条件

发布时间: 2012-03-11 18:15:39 作者: rapoo

帮看看这个Linq,怎么重复使用查询条件呢
用的是wcf ria和实体数据模型,请问如何修改能直接用同一判断过的条件query,再次查询

C# code
        private LoadOperation<V_Store> LoadSampleEntities()        {            this.CanLoad = false;            //return this.ds.Load(this.ds.GetV_StoreQuery().SortAndPageBy(this._view));            EntityQuery<V_Store> query = this.ds.GetV_StoreQuery();            if (!string.IsNullOrWhiteSpace(this.Tt_Company))                query = query.Where(e => e.co_Company.Contains(this.Tt_Company));            if (!string.IsNullOrWhiteSpace(this.Tt_Name))                query = query.Where(e => e.co_Name.Contains(this.Tt_Name));            if (!string.IsNullOrWhiteSpace(this.Tt_MPa))                query = query.Where(e => e.co_MPa.Contains(this.Tt_MPa));            if (!string.IsNullOrWhiteSpace(this.Tt_Spec))                query = query.Where(e => e.co_Spec.Contains(this.Tt_Spec));            if (!string.IsNullOrWhiteSpace(this.Tt_Mill))                query = query.Where(e => e.co_Mill.Contains(this.Tt_Mill));            if (!string.IsNullOrWhiteSpace(this.Tt_Remark))                query = query.Where(e => e.co_Remark.Contains(this.Tt_Remark));            if (!string.IsNullOrWhiteSpace(Tt_StartAmount))            {//数量                Decimal? SAmount = Decimal.Parse(Tt_StartAmount);                query = query.Where(e => e.co_Amount >= SAmount);            }            if (!string.IsNullOrWhiteSpace(Tt_EndAmount))            {                Decimal? EAmount = Decimal.Parse(Tt_EndAmount);                query = query.Where(e => e.co_Amount <= EAmount);            }            if (!string.IsNullOrWhiteSpace(Tt_StartWeight))            {//重量                Decimal? SWeight = Decimal.Parse(Tt_StartWeight);                query = query.Where(e => e.co_Weight >= SWeight);            }            if (!string.IsNullOrWhiteSpace(Tt_EndWeight))            {                Decimal? EWeight = Decimal.Parse(Tt_EndWeight);                query = query.Where(e => e.co_Weight <= EWeight);            }            if (!string.IsNullOrWhiteSpace(Dt_StartTime))            {//时间                DateTime? STime = DateTime.Parse(Dt_StartTime);                query = query.Where(e => e.co_UpDateTime >= STime);            }            if (!string.IsNullOrWhiteSpace(Dt_EndTime))            {                DateTime? ETime = DateTime.Parse(Dt_EndTime);                query = query.Where(e => e.co_UpDateTime <= ETime);            }            if (Ci_IsZero) query = query.Where(e => e.co_Amount != 0 && e.co_Weight != 0);            InvokeOperation<int> invokeOperation = ds.GetV_StoreWeightSum(query.SortAndPageBy(this._view)); 我想在这里再用上面的条件            return this.ds.Load(query.SortAndPageBy(this._view));        }


[解决办法]
(from y in db.ViewCompanys
where (
((!keywordIsEmpty && !nameIsEmpty && ((y.CompanyName.ToLower().Trim().Contains(keyword) || y.CompanyLongDescription.ToLower().Trim().Contains(keyword) || y.CompanyShortDescription.ToLower().Trim().Contains(keyword)) && (y.CompanyName.ToLower().Trim().Contains(name))))
|| (!keywordIsEmpty && nameIsEmpty && (y.CompanyName.ToLower().Trim().Contains(keyword) || y.CompanyLongDescription.ToLower().Trim().Contains(keyword) || y.CompanyShortDescription.ToLower().Trim().Contains(keyword)))
|| (!nameIsEmpty && keywordIsEmpty && (y.CompanyName.ToLower().Trim().Contains(name)))
|| (nameIsEmpty && keywordIsEmpty))
[解决办法]
C# code

Func<V_Store, bool> predicate = x => true;predicate = x => predicate(x) && x.co_Amount >= SAmount;...query = query.Where(x => predicate(x)); 

读书人网 >.NET

热点推荐