Tag: lambda

属性选择器和Where 使用Linq查询

我想这样做: public class SomeEntityClass { public Guid MyClassProperty {get;set;} } public class AnotherEntityClass { public Guid AnotherProperty {get;set;} } public T GetByProperty(Guid value, Expression<Func> selector) { return = Session.Query().Where(x => selector == value).FirstOrDefault(); } 应该叫: Repository.GetByProperty(Guid.NewGuid(), x => x.MyClassProperty ); Repository.GetByProperty(Guid.NewGuid(), x => x.AnotherProperty); 但它不起作用。 有帮助吗? 谢谢。

如何获取F的Func中使用的属性名称字符串

我有一个场景,我必须得到一个字符串数组,代表Func参数中使用的每个属性名称。 这是一个示例实现: public class CustomClass { public string[] GetPropertiesUsed { get { // do magical parsing based upon parameter passed into CustomMethod } } public void CustomMethod(Func method) { // do stuff } } 这是一个示例用法: var customClass = new CustomClass(); customClass.CustomMethod(src => “(” + src.AreaCode + “) ” + src.Phone); … var propertiesUsed = customClass.GetPropertiesUsed; […]

使用实体方法的结果来过滤lambda表达式或linq查询

我想根据使用其属性的函数的结果来过滤我的entites。 即。 我有这样的实体: public class Lorem { public int A {get;set;} public int B {get;set;} public int C {get;set;} public double DoMath(int externalValue) { // real implementation is longer and more complex if(A==B) { // some calculations return 0.2; } if(B==C) { // some calculations return 0.9; } else return 0; } } 现在我正在查询实体,我想只获得DoMath> 0的那些实体。 […]

将2个参数Lambda表达式转换为1个参数Lambda表达式(指定一个参数)

我有表达 Expression<Func> CanBeDrivenBy = (car, driver) => car.Category == ‘B’ && driver.Age > 18; 我想得到一些可以由一些司机驾驶的汽车 IQueryable cars = …; Driver driver = …; cars.Where(CanBeDrivenBy); // Fail, expecting Expression<Func> 所以我需要将Expression<Func>为Expression<Func> (指定驱动程序) 是的,我可以使用 cars.Where(c => c.Category == ‘B’ && driver.Age > 18); 但我需要能够动态改变表达式的解决方案。 我需要传递Expression(使用entity framework)

表达式树 – 如何获取声明实例?

当谈到表达树时,我是新手,所以我不确定如何提出这个问题或使用什么术语。 这是我正在尝试做的过于简化的版本: Bar bar = new Bar(); Zap(() => bar.Foo); public static void Zap(Expression<Func> source) { // HELP HERE: // I want to get the bar instance and call bar.Zim() or some other method. } 我怎样才能在Zap方法中使用bar?

C# – 查找两个List s – Lambda语法的公共成员

所以我写了这个简单的控制台应用程序,以帮助我的问题。 在方法的第3行使用lambda表达式来获取公共成员的正确方法是什么。 尝试了Join()但无法弄清楚正确的语法。 作为后续行动……有没有一种非LINQ方式在我错过的一行中做到这一点? class Program { static void Main(string[] args) { List c = new List() { 1, 2, 3 }; List a = new List() { 5, 3, 2, 4 }; IEnumerable j = c.Union(a); // just show me the Count Console.Write(j.ToList().Count.ToString()); } }

Expression.Lambda:变量”’类型”从范围”引用,但它没有定义

我看到了连接主题但是…… 我试图实现规范模式。 如果我使用System.Linq.Expressions API显式创建Or或And Expression,我将收到错误 从作用域引用的InvalidOperationExpression变量’x’。 例如,这是我的代码 public class Employee { public int Id { get; set; } } Expression<Func> firstCondition = x => x.Id.Equals(2); Expression<Func> secondCondition = x => x.Id > 4; Expression predicateBody = Expression.OrElse(firstCondition.Body, secondCondition.Body); Expression<Func> expr = Expression.Lambda<Func>(predicateBody, secondCondition.Parameters); Console.WriteLine(session.Where(expr).Count()); – //I got error here EDITED 我尝试使用Linq到Nhibernate的规范模式,所以在我的工作代码中它看起来像: ISpecification specification = new […]

在另一个lambda表达式中使用lambda表达式

我需要组合两个lambda表达式,第二个表达式“包含”第一个表达式。 我搜索了很多,但没有找到任何明确的答案…… 我要做的是以下内容:第一个表达式“expression1”作为参数传递给方法,并且仅用于定义第二个lambda必须在哪个字段或属性上运行。 原理上,我正在尝试执行以下操作: // simple field selector : Expression<Func> expression1 = obj => obj.field; // trying to use the field selector in 2nd expression : Expression<Func> e2 = obj => [[Result of expression1]].Equals(“myValue”); 换句话说,我想得到: Expression<Func> e2 = obj => obj.field.Equals(“myValue”); 我需要这样做,因为它并不总是被调用的Equals()方法,而是许多不同的方法。 我试图将expression1编译为Func以便在expression2中调用此Func,但是当我在LinQ中使用它时,我得到一个exception,因为LinQ不支持Invoke节点类型。 所以我的问题是:有没有办法将两个表达式的主体结合起来,请问有多喜欢? 谢谢提前!

OrderBy使用String keySelector

我有以下函数,它根据对象的属性(此处为Client)提取不同的值。 public List GetDistinctValues(string propertyName) { //how should I specify the keySelector ? Func keySelector = item => propertyName; var list = new List(); var values = this.ObjectContext.Clients.Select(CreateSelectorExpression (propertyName)).Distinct().OrderBy(keySelector); int i = 0; foreach (var value in values) { list.Add(new DistinctValue() { ID = i, Value = value }); i++; } return list; } private […]

如何使用lambda表达式和linq从范围中获取元素?

如何使用lambda和linq按范围获取元素? 例如: 我有一个包含54个元素的表。 我只是想从1-10或10-20或20-30等中取出元素 – 通常用一些数值范围。 我怎样才能做到这一点?