如何比较两个List的同名属性的值? 表示看不懂的说。。A,AS,Aa1,B,Bs,BBs12,34。。好凌乱。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Ass 是 List<As>,而As是List<A>, B也是这样的情况, 小写的都是实例 As又不是类型,哪来List<As>…… 数据结构上用Dictionary来放属性集合会方便一些。排版也比较清楚。static class Program{ static void Main(string[] args) { List<Constraint> constraints = new List<Constraint>() { new Constraint("Height", Rule.GreaterThan, 100), new Constraint("Name", Rule.Equals, "csdn"), }; Dictionary<string, object> p1 = new Dictionary<string, object>() { {"Height", 110}, {"Name", "csdn"}, {"Volume", 280}, {"Color", "red"}, }; Dictionary<string, object> p2 = new Dictionary<string, object>() { {"Height", 80}, {"Name", "csdn"}, {"Volume", 300}, {"Color", "red"}, }; List<Dictionary<string, object>> inputs = new List<Dictionary<string, object>>(){p1, p2}; List<Dictionary<string, object>> validEntities = inputs.Where(x => constraints.All(c => c.Match(x))).ToList(); System.Diagnostics.Debug.Assert(validEntities.Count == 1 && validEntities[0] == p1); // 验证,只有p1符合要求}enum Rule{ Equals, GreaterThan, LessThan,}class Constraint{ public Constraint(string name, Rule rule, object value) { this.PropertyName = name; this.Rule = rule; this.Value = value; } public string PropertyName { get; private set; } public Rule Rule { get; private set; } public object Value { get; private set; } public bool Match(IDictionary<string, object> bag) { if (bag != null && bag.ContainsKey(this.PropertyName)) { int diff = System.Collections.Comparer.Default.Compare(bag[PropertyName], this.Value); if (diff == 0 && this.Rule == Rule.Equals) return true; if (diff < 0 && this.Rule == Rule.LessThan) return true; if (diff > 0 && this.Rule == Rule.GreaterThan) return true; } return false; }} 学习了,这个排版太好了。 分都给你吧。另外我已经通过循环套循环的方法, 把List都遍历进行了比对, 然后输出了结果 Dictionary确实很好用, 但我不会把这个作为Class定义 在大型project里面不知道怎么作为一个独立出来的能随时修改的部分进行定义呢? 一个字符串怎么序列化到XML文件中,想知道有不有简单的方法 询问WPF程序通过网页打开后,指纹仪API调用有问题。 SqlDataAdapter怎么重复使用 关于listView的问题~~ 生成的静态HTML页面能不能像DataGrid一样分页?? 100散分了~!!!大家来啊。。 怎么样Form类放入到panel中? 求一个正则,挺难的,望高手来指点~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 强烈推荐C#的XDJM看看Free C#的IDE,附源码 关于控件第一次加载速度较慢 求一个导出word 方案 c# socket通信的问题
Ass 是 List<As>,而As是List<A>, B也是这样的情况, 小写的都是实例
{
static void Main(string[] args)
{
List<Constraint> constraints = new List<Constraint>()
{
new Constraint("Height", Rule.GreaterThan, 100),
new Constraint("Name", Rule.Equals, "csdn"),
}; Dictionary<string, object> p1 = new Dictionary<string, object>()
{
{"Height", 110},
{"Name", "csdn"},
{"Volume", 280},
{"Color", "red"},
}; Dictionary<string, object> p2 = new Dictionary<string, object>()
{
{"Height", 80},
{"Name", "csdn"},
{"Volume", 300},
{"Color", "red"},
}; List<Dictionary<string, object>> inputs = new List<Dictionary<string, object>>(){p1, p2};
List<Dictionary<string, object>> validEntities = inputs.Where(x => constraints.All(c => c.Match(x))).ToList();
System.Diagnostics.Debug.Assert(validEntities.Count == 1 && validEntities[0] == p1); // 验证,只有p1符合要求
}
enum Rule
{
Equals,
GreaterThan,
LessThan,
}class Constraint
{
public Constraint(string name, Rule rule, object value)
{
this.PropertyName = name; this.Rule = rule; this.Value = value;
}
public string PropertyName { get; private set; }
public Rule Rule { get; private set; }
public object Value { get; private set; }
public bool Match(IDictionary<string, object> bag)
{
if (bag != null && bag.ContainsKey(this.PropertyName))
{
int diff = System.Collections.Comparer.Default.Compare(bag[PropertyName], this.Value);
if (diff == 0 && this.Rule == Rule.Equals) return true;
if (diff < 0 && this.Rule == Rule.LessThan) return true;
if (diff > 0 && this.Rule == Rule.GreaterThan) return true;
}
return false;
}
}
学习了,这个排版太好了。 分都给你吧。
另外我已经通过循环套循环的方法, 把List都遍历进行了比对, 然后输出了结果 Dictionary确实很好用, 但我不会把这个作为Class定义 在大型project里面不知道怎么作为一个独立出来的能随时修改的部分进行定义呢?