日期:2014-05-18 浏览次数:20931 次
//有三个文本框,手动输入数据 var data1 = textbox1.Text; var data2 = textbox2.Text; var data3 = textbox3.Text; Func<string, bool> Filter1 = x => x.Any(y => data1.Contains(y)); Func<string, bool> Filter2 = x => x.Any(y => data2.Contains(y)); Func<string, bool> Filter3 = x => x.Any(y => data3.Contains(y)); //如果文本框不为空,单独一个文本框输入对应的输出分别为: //第一个文本框 var result = Enumerable.Range(0, 1000).Where(x => Filter1(x.ToString().PadLeft(3, '0'))); //第二个文本框 var result = Enumerable.Range(0, 1000).Where(x => Filter2(x.ToString().PadLeft(3, '0'))); //第三个文本框 var result = Enumerable.Range(0, 1000).Where(x => Filter3(x.ToString().PadLeft(3, '0'))); //如果三个文本框都有数据输入,则输出result为上述三个输出的交集 Func<string, bool> Filter = x => true; Filter = x => Filter1(x) && Filter2(x) && Filter3(x); var result = Enumerable.Range(0, 1000).Where(x => Filter(x.ToString().PadLeft(3, '0'))); //如果三个文本框有空的情况,上述的result输出就会为空,这个时候需要判断哪几个文本框输入不为空,对输入不为空的文本框输出进行交集处理,此时如果仅仅只有三个文本框,则用If判断后组合交集也比较方便,但是效率低 //如果有N个文本框,对应上述类似情况,该怎么改?
Func<string, bool> Filter1 = x => string.IsNullOrEmpty(data1) || x.Any(y => data1.Contains(y)); Func<string, bool> Filter2 = x => string.IsNullOrEmpty(data2) || x.Any(y => data2.Contains(y)); Func<string, bool> Filter3 = x => string.IsNullOrEmpty(data3) || x.Any(y => data3.Contains(y));