位标志有点难以理解:)
我知道这个和这个问题,我确实理解答案,我甚至从我的一个好朋友那里听过这篇文章.
但是当我需要"渐进"超过标准时,我仍然无法弄明白......
我想要做的是这样的:
if (HttpContext.Current.Session["DebugSessionText"] != null)
{
showType = parDebug.Write2LogType.WARN |
parDebug.Write2LogType.ERROR |
parDebug.Write2LogType.INFO;
if (!chkInfo.Checked)
showType &= ~parDebug.Write2LogType.INFO; // remove INFOs
if (!chkError.Checked)
showType &= ~parDebug.Write2LogType.ERROR; // remove ERRORs
List<myDebugRow> list =
(List<myDebugRow>)HttpContext.Current.Session["DebugSessionText"];
gv.DataSource = list.FindAll(x => x.Type == showType));
}
gv.DataBind();
Run Code Online (Sandbox Code Playgroud)
我确实需要过滤一个List对象,所以我可以得到用户想要的东西(仅显示INFO错误,异常错误,但始终会显示WARNing错误)...
有没有直接的方法来做到这一点,或者我需要手动过滤它而不使用LAMBDA表达式?
谢谢你的帮助.
Hen*_*man 10
同
x.Type == showType
Run Code Online (Sandbox Code Playgroud)
您将只获得完全符合所有条件(位标志)的项目.同
(x.Type & showType) != 0
Run Code Online (Sandbox Code Playgroud)
您将找到与showType至少匹配1位的所有项目,这可能是您想要的.
如果您发现这些操作令人困惑 - 坦率地说,我当然这样做 - 那么考虑提高抽象级别.给自己写一些辅助扩展方法.
static WriteToLogType AddWarn(this WriteToLogType x) { return x | WriteToLogType.WARN; }
static WriteToLogType ClearWarn(this WriteToLogType x) { return x & ~WriteToLogType.WARN; }
static bool HasWarn(this WriteToLogType x) { return 0 != (x & WriteToLogType.WARN); }
// same for Error, Info
static bool HasExactly(this WriteToLogType x, WriteToLogType y) { return x == y; }
static bool HasAny(this WriteToLogType x, WriteToLogType y) { return 0 != (x & y); }
static bool HasAll(this WriteToLogType x, WriteToLogType y) { return y == (x & y); }
Run Code Online (Sandbox Code Playgroud)
现在你的程序就变成了
showType = WriteToLogType.None.AddWarn().AddInfo().AddError();
if (!chkInfo.Checked) showType = showType.ClearInfo();
if (!chkError.Checked) showType = showType.ClearError();
List<myDebugRow> list = whatever;
gv.DataSource = list.FindAll(x => x.Type.HasAny(showType)));
Run Code Online (Sandbox Code Playgroud)
我希望你们同意这一点比所有那些令人讨厌的事情要清楚得多.但我们仍然可以更清楚地说明这一点.
showType = WriteToLogType.None.AddWarn();
if (chkInfo.Checked) showType = showType.AddInfo();
if (chkError.Checked) showType = showType.AddError();
List<myDebugRow> list = whatever;
gv.DataSource = list.FindAll(x => x.Type.HasAny(showType)));
Run Code Online (Sandbox Code Playgroud)
而不是添加一堆标志,然后将它们带走,而不是首先添加它们.