- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个页面名称为:CustomerList.aspx,我在上面显示客户列表。
这也是我的表和类文件:
public partial class Customer
{
public int CustomerID { get; set; }
public string FullName { get; set; }
public string EmailId { get; set; }
public int CustomerLocation { get; set; }
public bool IsActive { get; set; }
public bool Removed { get; set; }
public DateTime SubscribeDate { get; set; }
public Location _Location;
}
public partial class Location
{
public int LocationId { get; set; }
public string Name { get; set; }
}
Inactive=true:Means customer is active in the system.
Inactive=false:Means customer is inactive in the system.Removed=true:Means customer is removed from the system
Removed=false:Means customer is not removed from the system.
我会为用户提供 3 个过滤器来过滤客户记录。
1)位置下拉菜单
<select>
<option Text="All" Value="0" selected="true">
<option Text="London" Value="1">
<option Text="America" Value="2">
</select>
2)状态下拉列表,值为:All、Active、Inactive:
<select>
<option Text="All" Value="0" selected="true">
<option Text="Active" Value="1">
<option Text="Inactive" Value="2">
</select>
3)统计数据下拉列表:
<select>
<option Text="All" Value="all" selected="true">
<option Text="Active Customers" Value="all">
<option Text="Recent subscribe customers" Value="subscribe">
<option Text="Recent unsubscribe customers" Value="unsubscribe">
</select>
加载我的页面时,我想在我的网格中显示客户列表。
这是我的代码:
public void DisplayCustomersList()
{
DataTable list=GetCustomers(Convert.ToInt16(ddlLocation.SelectedValue),Convert.ToInt16(ddlStatus.SelectedValue),ddlstats.SelectedValue);
Grid1.DataSource = list;
Grid1.DataBind();
}
public DataTable GetCustomers(int LocationId, int ActiveId, string stats)
{
using (var context = new MyContext())
{
var data = from c in context.Customers
where c.Removed == false
select new
{
FullName = c.FullName,
c.CustomerID,
c._Location.Name,
c.IsActive,
c.SubscribeDate,
c.Removed
};
if (LocationId != 0 && ActiveId != 0)
{
if (ActiveId == 1)
{
return
MyContext.CopyToDataTable(
data.Where(x => x.LocationId == LocationId && x.IsActive == true && x.Removed == false));
}
else if(ActiveId==2)
{
return
MyContext.CopyToDataTable(
data.Where(x => x.LocationId == LocationId && x.IsActive == false && x.Removed == false));
}
return
MyContext.CopyToDataTable(
data.Where(x => x.LocationId == LocationId && x.Removed==false));
}
if (LocationId != 0 && stats != "")
{
if (stats == "all")
{
return
MyContext.CopyToDataTable(
data.Where(x => x.LocationId == LocationId && x.IsActive == true && x.Removed == false));
}
else if (stats == "subscribe")
{
return
MyContext.CopyToDataTable(
data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.LocationId==LocationId));
}
}
if (ActiveId != 0 && stats != "")
{
if(ActiveId==1)
{
if(stats=="all")
{
return
MyContext.CopyToDataTable(
data.Where(x => (x.SubscribeDate >= DateTime.Now.AddDays(-7) || x.IsActive == true) || (x.Removed == false) || (x.SubscribeDate >= DateTime.Now.AddDays(-7) || x.IsActive == false)));
}
else if (stats == "subscribe")
{
return
MyContext.CopyToDataTable(
data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.IsActive == true));
}
else if (stats == "unsubscribe")
{
return
MyContext.CopyToDataTable(
data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.IsActive == false));
}
}
else if(ActiveId==2)
{
if (stats == "all")
{
MyContext.CopyToDataTable(
data.Where(x => (x.SubscribeDate >= DateTime.Now.AddDays(-7) || x.IsActive == false) && (x.Removed == false)));
}
else if (stats == "subscribe")
{
return
MyContext.CopyToDataTable(
data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.IsActive == false));
}
else if (stats == "unsubscribe")
{
return
MyContext.CopyToDataTable(
data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.IsActive == false));
}
}
}
if (stats != "")
{
if (stats == "all")
{
return
MyContext.CopyToDataTable(
data.Where(x => x.IsActive == true && x.Removed == false));
}
else if (stats == "subscribe")
{
return
MyContext.CopyToDataTable(
data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.IsActive==true));
}
else
{
return
MyContext.CopyToDataTable(
data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.IsActive == false && x.Removed == false));
}
}
}
}
在所有 3 dropdown selected index change event
我只是像这样调用这个函数:DisplayCustomersList()
所以我只想问你这是执行过滤器的正确方法还是可以以更好的方式优化此代码。
谁能给我提供更好的解决方案或以更好的方式优化这段代码???
最佳答案
这个问题可以在没有PredicateBuilder
的情况下进行优化,但是它需要仔细和“系统”的分析。
考虑你的情况,像这样将所有 14 个放在一起,你实际上可以看到你只有 三个 决定因素,即:LocationId
,ActiveId
和 stats
:
No LocationId ActiveId stats result
1 not 0 1 don't care data.Where(x => x.LocationId == LocationId && x.IsActive == true && x.Removed == false)
2 not 0 2 don't care data.Where(x => x.LocationId == LocationId && x.IsActive == false && x.Removed == false));
3 not 0 not 0-2 don't care data.Where(x => x.LocationId == LocationId && x.Removed == false));
4 not 0 don't care all data.Where(x => x.LocationId == LocationId && x.IsActive == true && x.Removed == false)
5 not 0 don't care subscribe data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.LocationId == LocationId));
6 don't care 1 all data.Where(x => (x.SubscribeDate >= DateTime.Now.AddDays(-7) || x.IsActive == true) || (x.Removed == false) || (x.SubscribeDate >= DateTime.Now.AddDays(-7) || x.IsActive == false)));
7 don't care 1 subscribe data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.IsActive == true));
8 don't care 1 unsubscribe data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.IsActive == false));
9 don't care 2 all data.Where(x => (x.SubscribeDate >= DateTime.Now.AddDays(-7) || x.IsActive == false) && (x.Removed == false)));
10 don't care 2 subscribe data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.IsActive == false));
11 don't care 2 unsubscribe data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.IsActive == false));
12 don't care don't care all data.Where(x => x.IsActive == true && x.Removed == false));
13 don't care don't care subscribe data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.IsActive == true));
14 don't care don't care unsubscribe data.Where(x => x.SubscribeDate >= DateTime.Now.AddDays(-7) && x.Removed == false && x.IsActive == false));
我观察到您的结果非常确定,只有少数异常(exception)。除了结果 no 6
和 no 9
,您的查询谓词实际上可以分为四个 基本组件(6
和 9
省略)。它们是:
comp1: x.LocationId == LocationId
comp2: x.IsRemoved == false
comp3: x.IsActive == true
comp4: x.SubscribeDate >= DateTime.Now.AddDays(-7)
查询逻辑很简单:
comp1 && comp2 && comp3 && comp4
将它们与 12 个案例(不包括案例 6
和 9
)放在一起,您将得到:
Simplification:
DC = don't care
A = applied
NA = not applied
QueryComponents
No LocationId ActiveId stats comp1 comp2 comp3 comp4
1 not 0 1 DC A A Yes NA
2 not 0 2 DC A A No NA
3 not 0 not 0-2 DC A A NA NA
4 not 0 DC all A A Yes NA
5 not 0 DC subscribe A A NA A
7 DC 1 subscribe NA A Yes A
8 DC 1 unsubscribe NA A No A
10 DC 2 subscribe NA A No A
11 DC 2 unsubscribe NA A No A
12 DC DC all NA A Yes A
13 DC DC subscribe NA A Yes A
14 DC DC unsubscribe NA A No A
现在,可以看出查询组件可以与决定因素一起映射回来:
comp1: Applied only when LocationId is not 0
comp2: Always applied //this is very good!
comp3: Yes = 1, 4, 7, 12, 13; NA = 3, 5; No = 2, 8, 10, 11, 14
comp4: Not Applied when LocationId is 0 except on case 5
因此,我们可以制作一些帮助标志(共有 4 个)来确定是否应包含查询组件,如下所示:
bool LocationIdNotApplied = LocationId == 0; //for comp1
bool IsActiveNotApplied = LocationId != 0 && (ActiveId < 0 || ActiveId > 2 || stats = "subscribe"); //for comp3 to be applied or not
bool IsActiveFalse = (LocationId != 0 && ActiveId == 2) || stats == "unsubscribe" || (ActiveId == 2 && stats == "subscribe"); //for comp3 to be false
bool DateApplied = LocationId == 0 || (LocationId != 0 && stats == "subscribe");
然后你的 data.Where
除了 6
和 9
之外的所有情况都可以像这样简化:
data.Where(x => (x.LocationId == LocationId || LocationIdNotApplied) //comp1
&& x.IsRemoved == false //comp2
&& ((x.IsActive == !IsActiveFalse) || IsActiveNotApplied) //comp3
&& (x.SubscribeDate >= DateTime.Now.AddDays(-7) || !DateApplied)) //comp4
这是将 12 个案例简化为 1 个案例的重大简化,您只需添加额外的两个案例,总共 3 个案例,而不是原来的 14 个案例!
public DataTable GetCustomers(int LocationId, int ActiveId, string stats)
{
using (var context = new MyContext())
{
var data = from c in context.Customers
where c.Removed == false
select new
{
FullName = c.FullName,
c.CustomerID,
c._Location.Name,
c.IsActive,
c.SubscribeDate,
c.Removed
};
bool LocationIdNotApplied = LocationId == 0; //for comp1
bool IsActiveNotApplied = LocationId != 0 && (ActiveId < 0 || ActiveId > 2 || stats = "subscribe"); //for comp3 to be applied or not
bool IsActiveFalse = (LocationId != 0 && ActiveId == 2) || stats == "unsubscribe" || (ActiveId == 2 && stats == "subscribe"); //for comp3 to be false
bool DateApplied = LocationId == 0 || (LocationId != 0 && stats == "subscribe");
if(LocationId == 0 && ActiveId == 1 && stats == "all"){ //case 6
return MyContext.CopyToDataTable(
data.Where(x => (x.SubscribeDate >= DateTime.Now.AddDays(-7) || x.IsActive == true) || (x.Removed == false) || (x.SubscribeDate >= DateTime.Now.AddDays(-7) || x.IsActive == false)));
} else if (LocationId == 0 && ActiveId == 2 && stats == "all"){ //case 9
return MyContext.CopyToDataTable(
data.Where(x => (x.SubscribeDate >= DateTime.Now.AddDays(-7) || x.IsActive == false) && (x.Removed == false)));
} else { //other cases
return MyContext.CopyToDataTable(
data.Where(x => (x.LocationId == LocationId || LocationIdNotApplied) //comp1
&& x.IsRemoved == false //comp2
&& ((x.IsActive == !IsActiveFalse) || IsActiveNotApplied) //comp3
&& (x.SubscribeDate >= DateTime.Now.AddDays(-7) || !DateApplied))) //comp4
}
}
}
你的情况 6 实际上对我来说很奇怪:
data.Where(x => (x.SubscribeDate >= DateTime.Now.AddDays(-7) || x.IsActive == true) || (x.Removed == false) || (x.SubscribeDate >= DateTime.Now.AddDays(-7) || x.IsActive == false)));
请注意,对于 x.SubscribeDate >= DateTime.Now.AddDays( -7)
。然后将它与 ||
组合。这就像在说:
(A || true) || (A || false)
并且无论如何都会返回true
。您可能想再次检查,甚至可以进一步简化/
因此,我针对这种情况的解决方案没有 PredicateBuilder
- 它需要对所有可能的情况进行仔细和“系统的”(或者,我的实际意思是逐步)分析。
我必须向 OP 道歉,因为我无法完全测试我提出的代码,因为缺乏完整的测试资源(与 OP 不同)。
但是如果 OP 发现有一个我没有处理的情况 或者 没有被 OP 放在原始问题中,至少,我上面的解决方案中提出的步骤应该对于 OP 对他/她的实际案例进行他/她自己的仔分割析仍然有用。
关于c# - 通过 3 个下拉过滤器过滤客户记录的更好或优化方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35332928/
我有一个名为 flightbooking 的表。该表由字段组成,包括标有“R”或“C”的状态、已保留和已取消。 我想弄清楚的是允许删除状态为“C”的记录,但如果下一行的状态为“R”,则不应删除。 [编
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
研究 PHP/Gearman。我试图了解 Gearman 服务器/进程如何确定什么构成“有效”客户端。 在我看过的文档中,文档显示了许多连接到 Gearman 服务器的客户端。但是,我没有找到任何描述
HBase案例:客户/订单 假设HBase 用于存储客户和订单信息。有两种核心记录类型被摄取:客户记录类型和订单记录类型。 客户记录类型将包含您通常期望的所有内容: 客户编号 客户名称
当运行配方时出现问题并且客户端运行中途挂起时,安装的 Chef 客户端将无法使用。 然后,您可以退出机器、重新启动、清理 Chef pid 文件等,但每次启动 Chef 客户端时都会显示以下消息: C
我正在用Java开发游戏,目前进展顺利。我想尽早实现多人游戏,因此我在此基础上进行构建,而不是在具有大量不同功能的情况下将整个游戏移植到多人游戏中。 我想使其成为客户端/服务器应用程序。 现在,我确定
我正在尝试以编程方式修改 magento 上的现有客户数据,但出现错误,希望有人可以帮助我。 require_once('../app/Mage.php'); ini_set("error_repor
我正在学习 JQuery。我需要创建一个自定义控件。该控件基本上将呈现一些 HTML。有时,我只想获取 HTML。我希望使用以下语法: // Put generated html inside of
我正在尝试以编程方式修改 magento 上的现有客户数据,但出现错误,希望有人可以帮助我。 require_once('../app/Mage.php'); ini_set("error_repor
我想选择表格的一行并插入到另一个现有表格中。(我用的是php和MySql,两张表除了id和timestamp外,其他列都是一样的) 我试着做一个手推车。我希望当有人付款时,从“carret”中选择该客
我想使用 JXTA 实现客户端-服务器架构。服务器部分的配置也将是 3 层。有谁知道开始这个项目的好地方吗? 提前致谢。 最佳答案 JXTA 处理 p2p 连接。这与客户端-服务器方法相反。 据我了解
我需要创建一个 Stripe 客户并为他们订阅一个计划。我查看了 Stripe.js,似乎虽然您可以获得 token ,但除此之外它没有提供任何东西。有可用于节点和 PHP 的库,但为什么不能用于 J
我有一个数据库设计要求,它变得越来越复杂,我需要找出最佳的处理方法。 目前,我有“联系人”,也有“公司”。一个公司可以有多个联系人,但一个联系人只能属于一个公司。这形成了明显的 1:n 关系。但是,联
http://ecc.galengrover.com/programs/facility-rentals/ 在那个页面上,只有那个页面我有一个奇怪的问题。在内容框上方,“所有区域”一词出现在其他文本的
所以,我让这个 CSS 预加载器正常工作。但是,我在将其添加到我的网站时遇到了一些困难。有人可以帮助我理解代码的实现,以允许预加载器成为网站加载前查看的第一个元素。此时预加载器将淡出。预加载器的代码如
考虑以下场景: 有一个 Windows 服务托管一个 WCF 服务。 WCF 服务在客户端和位于不同物理机器上的 AppFabric 服务器之间提供接口(interface)。它检索对象,对其进行计算
当前端的订单历史页面 (*/sales/order/history/) 只显示一条短消息时,这个问题首先被意识到:您没有下订单。 经过一番调试,发现问题出在这个函数上: Mage::getSingle
我的公司是一些项目的定制开发商店,有些较大,有些较小。目前,我们通过电子邮件处理所有客户通信。所以我们通过电子邮件发送设计文档,他们将其标记并发送回去。然后我们推出了他们产品的测试版,他们通过电子邮件
我有一个小部件 View 如下: 公共(public)类 RemoteNumView 扩展了 FrameLayout { 我如何调用 Roboguice 就像在 RoboActivity 中一样?如下
我有一个包含客户编号、客户名称和商品编号的 MySQL 表。它比这稍微复杂一点,但我尝试稍微简化它并删除敏感数据。 当前表 Unique Customer Number | Customer Name
我是一名优秀的程序员,十分优秀!