当前位置: 首页>>代码示例>>C#>>正文


C# QueryBuilder.IfThenElse方法代码示例

本文整理汇总了C#中QueryBuilder.IfThenElse方法的典型用法代码示例。如果您正苦于以下问题:C# QueryBuilder.IfThenElse方法的具体用法?C# QueryBuilder.IfThenElse怎么用?C# QueryBuilder.IfThenElse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QueryBuilder的用法示例。


在下文中一共展示了QueryBuilder.IfThenElse方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: btnQuery_Click

        private void btnQuery_Click(object sender, EventArgs e)
        {
            List<Product> productList = new List<Product>();
            for (int i = 0; i < 10; i++)
            {
                Product product = new Product();
                product.Id = i + 1000;
                product.Name = "Cost " + i.ToString();
                Random random = new Random(i);

                product.Quotation = new Quotation();
                product.Quotation.Info = new Information();
                product.Quotation.StartingPrice = 1000 + i;
                product.Quotation.Info.Number = product.Id;

                product.AnnualQuotation = new AnnualQuotation();
                product.AnnualQuotation.StartingPrice = 5000 + i;

                product.RecommandLevel = i;
                product.Type = (i % 2 == 0) ? productType.Annual :
                                              productType.NonAnnual;

                productList.Add(product);
            }

            ProductRequest request = new ProductRequest();
            request.IsAnnualCover = true;
            request.OrderType = 1;

            IQueryBuilder<Product> qb = new QueryBuilder<Product>();

            var annualPriceOrderPredicateCases = new Dictionary<int, Expression<Func<Product, dynamic>>>();
            var annualPriceSortOrderCases = new Dictionary<int, SortOrder>();

            annualPriceOrderPredicateCases.Add(0, (p)=>p.AnnualQuotation != null ? p.AnnualQuotation.StartingPrice : p.Id);
            annualPriceOrderPredicateCases.Add(1, (p)=>p.AnnualQuotation != null ? p.AnnualQuotation.StartingPrice : p.Id);
            annualPriceOrderPredicateCases.Add(2, (p)=>p.RecommandLevel);

            annualPriceSortOrderCases.Add(0, SortOrder.Ascending);
            annualPriceSortOrderCases.Add(1, SortOrder.Descending);
            annualPriceSortOrderCases.Add(2, SortOrder.Descending);

            //qb.SwitchOrderBy(() => request.OrderType, annualPriceOrderPredicateCases, annualPriceSortOrderCases);

            var nonAnnualOrderPredicateCases = new Dictionary<int, Expression<Func<Product, dynamic>>>();
            var nonAnnualPriceSortOrderCases = new Dictionary<int, SortOrder>();

            nonAnnualOrderPredicateCases.Add(1, p => p.Quotation != null ? p.Quotation.StartingPrice : p.Id);
            nonAnnualOrderPredicateCases.Add(2, p => p.Quotation != null ? p.Quotation.StartingPrice : p.Id);
            nonAnnualOrderPredicateCases.Add(3, p => p.RecommandLevel);

            nonAnnualPriceSortOrderCases.Add(0, SortOrder.Ascending);
            nonAnnualPriceSortOrderCases.Add(1, SortOrder.Descending);
            nonAnnualPriceSortOrderCases.Add(2, SortOrder.Descending);

            //qb.SwitchOrderBy(() => request.OrderType, nonAnnualOrderPredicateCases, nonAnnualPriceSortOrderCases);

            IList<Product> pListOrdered =
            qb.IfThenElse(() => request.IsAnnualCover,
                          () => qb.SwitchOrderBy(() => request.OrderType, annualPriceOrderPredicateCases, annualPriceSortOrderCases),
                          () => qb.SwitchOrderBy(() => request.OrderType, nonAnnualOrderPredicateCases, nonAnnualPriceSortOrderCases))
                          .ToList(productList);

            var annualPriceCases = new Dictionary<int, Expression<Func<Product, bool>>>();

            annualPriceCases.Add(1, p => p.Quotation.StartingPrice > 1003);

            annualPriceCases.Add(2, p => p.Quotation.StartingPrice > 1004);

            IList<Product> pList1 =  qb.Switch(()=>request.OrderType, annualPriceCases).ToList(productList);

            IList<Product> pList2 = qb.Filter(p => p.Id > 1000).
                                   And(p => p.Quotation.StartingPrice > 1002).
                                   OrderBy(p=>p.AnnualQuotation.StartingPrice).
                                   ToList(productList.AsQueryable());
        }
开发者ID:daywrite,项目名称:EApp,代码行数:76,代码来源:Cost.cs


注:本文中的QueryBuilder.IfThenElse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。