十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
public System.Linq.Expressions.ExpressionFuncTElement, bool ContainsExpressionTElement, TValue(System.Linq.Expressions.ExpressionFuncTElement, TValue valueSelector, IEnumerableTValue values)
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网络空间、营销软件、网站建设、饶河网站维护、网站推广。
{
if (valueSelector == null)
{
throw new ArgumentNullException("valueSelector");
}
if (values == null)
{
throw new ArgumentNullException("values");
}
System.Linq.Expressions.ParameterExpression p = valueSelector.Parameters.Single();
if (!values.Any())
return e = false;
var @equals = values.Select(value = (System.Linq.Expressions.Expression)System.Linq.Expressions.Expression.Equal(valueSelector.Body, System.Linq.Expressions.Expression.Constant(value, typeof(TValue))));
var body = @equals.Aggregate((accumulate, equal) = System.Linq.Expressions.Expression.Or(accumulate, equal));
return System.Linq.Expressions.Expression.LambdaFuncTElement, bool(body, p);
}
实现有问题。在这里,C不需要实现 IEquatableC, 因为Except方法默认情况下调用的是 object 的 Equals 方法,而不是这个接口的方法。C改成如下内容即可:
public class C
{
public string S;
public override bool Equals(object other)
{
if(other==null)return false;
if(other.GetType()!=GetType())return false;
if(other==this)return true;
return ((C)other).S.Equals(S);
}
public override int GetHashCode()
{
return S.GetHashCode();
}
}
另外GetHashCode重写是一个建议,不是必须的,所以不会出错。
function Newfile as fileinfo
Dim dic As New System.IO.DirectoryInfo("C:\Users\Q\Desktop\数据")
Dim files = dic.GetFiles.OrderByDescending(Function(s) s.CreationTime)
if files.
return files.first
end function
注意引入 system.linq
见以下示例
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyApplication
{
class Program
{
public class MyClass
{
public string Name { get; set; }
public bool IsTrue { get; set; }
}
static void Main(string[] args)
{
Dictionaryint, MyClass dic =
new Dictionaryint, MyClass();
//添加
dic.Add(1, new MyClass { Name = "张三", IsTrue = false });
dic.Add(2, new MyClass { Name = "张四", IsTrue = true });
dic.Add(3, new MyClass { Name = "张五", IsTrue = false });
dic.Add(4, new MyClass { Name = "张六", IsTrue = true });
dic.Add(5, new MyClass { Name = "张七", IsTrue = false });
int c1 = dic.Count(e = e.Value.IsTrue == true);
Console.WriteLine(c1); //输出2
int c2 = dic.Count(e = e.Value.IsTrue == false);
Console.WriteLine(c2); //输出3
// 或者
var qry1 = from x in dic
where x.Value.IsTrue == true
select x;
int c3 = qry1.Count();
Console.WriteLine(c3); //输出2
var qry2 = from x in dic
where x.Value.IsTrue == false
select x;
int c4 = qry2.Count(); //输出3
Console.WriteLine(c4);
}
}
}
在.NET 中微软已经为我们内置了一些方法,我们可以直接使用该方法以快速达到我们的目的
Module Module1
Sub Main()
Dim a(3) As Int32
a(0) = 1
a(1) = 1
a(2) = 1
a(3) = 1
'推荐第一种方法
If a.Distinct().Count() = 1 Then
If a.All(Function(c) c = 1) Then
End If
End If
End Sub
End Module
第二种方法为LINQ,这是一种高级用法;是.NET 程序员必知必会的东西,如果不懂怎么用可以按F1查看官方文档
C# 版的
var datatable = new[] {
new {name = "aa", num = 5},
new {name = "aa", num = 6},
new {name = "aa", num = 11},
new {name = "ab", num = 1},
new {name = "ab", num = 8},
};
var result = datatable.GroupBy(o = o.name).Select(o = new { name = o.Key, num = (datatable.Where(p = p.name == o.Key).Select(r = r.num).Max() - datatable.Where(q = q.name == o.Key).Select(x = x.num).Min()) });
理解含义,用VB写一样的