Tag: linq

使用LINQ从字符串中删除字符

我正在尝试通过编写一些简单的扩展方法来刷新我的LINQ。 有没有更好的方法来编写如下函数从字符串中删除给定的字符列表(使用LINQ)? 它帮助我思考LINQ首先依赖的扩展方法: public static string Remove(this string s, IEnumerable chars) { string removeChars = string.Concat(chars); return new string(s.ToCharArray().Where(c => !removeChars.Contains(c)).ToArray()); } 但那很难看。 Ergo LINQ。 我在LINQ语句中注意到的差异是我必须使用’select’而使用扩展方法,我不必这样做。 /// Strip characters out of a string. /// The characters to remove. public static string Remove(this string s, IEnumerable chars) { string removeChars = string.Concat(chars); var stripped = from […]

无法将lambda表达式转换为类型“object”,因为它不是委托类型

我有一个具有bool属性的基类,如下所示: public abstract class MyBaseClass { public bool InProgress { get; protected set; } } 我从它inheritance了另一个类,并尝试将InProgress添加为字典的委托。 但它给我一个错误。 这就是我的Derived类的样子: public abstract class MyClass { Dictionary dict = new Dictionary(); dict.Add(“InProgress”, InProgress => base.InProgress = InProgress); } 这是我得到的错误: 无法将lambda表达式转换为类型“object”,因为它不是委托类型 我在这做错了什么?

linq从儿童collections中选择项目

以下是我的课程。 我有一个包含天数列表的产品。 每天都有城市房产。 我需要创建一个linq查询,它将为我提供在系统中所有产品上使用的不同城市。 我试过这样的东西,但它不起作用: var cities = from product in NHibernateSession.Linq() select new { city = product.Days.Where(d => d.City != null).Distinct() }; //This returns the day items but i need distinct cities public class Product : EntityBase { public virtual string Name { get; set; } public virtual IList Days { get; set; } […]

c#中的逻辑评估器(评估逻辑(&&,||)表达式)

在我的项目中有一个Logic evaluation部分,它将输入作为包含logical expressions ( true/false )的字符串。 我想评估此字符串并返回最终的布尔值。 string Logic=”1&0|1&(0&1)” //string Logic=”true AND false OR true AND (false AND true)” 这将是我的Logic 。 长度可能会增加。 有没有办法从LINQ / Dynamic LINQ评估这个表达式?

如何使用另一个也从查询中提取的对象的值来设置linq查询中所有项的属性?

我有一个从数据库中提取的查询: List items = new List(from i in context select new myClass { A = iA, B = “”, // i doesn’t know this, this comes from elsewhere C = iC } 我还有另一个查询做类似的事情: List otherItems = new List(from j in context select new myClass2 { A = jA, // A is the intersection, there will […]

有没有办法在lambda表达式树中使用`dynamic`?

一,规格。 我们使用MVC5,.NET 4.5.1和Entity框架6.1。 在我们的MVC5业务应用程序中,我们有很多重复的CRUD代码。 我的工作是“自动化”大部分内容,这意味着将其提取到基类并使其可重用。 现在,我有控制器,视图模型和EF6实体模型的基类。 我的抽象基类,所有EF6实体都inheritance: public abstract class BaseEntity where TSubclass : BaseEntity { public abstract Expression<Func> UpdateCriterion(); } UpdateCriterion方法用于数据库上下文的AddOrUpdate方法。 我有一个子类的generics参数,因为UpdateCriterion需要返回使用精确子类类型的lambda表达式,而不是接口或基类。 实现此抽象基类的极其简化的子类如下所示: public class Worker : BaseEntity { public int ID { get; set; } public int Name { get; set; } public override Expression<Func> UpdateCriterion() { return worker => worker.ID; } } […]

Linq to Entity从DateTime获取日期

var islemList =(来自entities中的isl .Islemler,其中( isl.KayitTarihi.Date > = dbas && isl.KayitTarihi.Value.Date <= dbit)select isl); 它给出了错误:LINQ to Entities不支持日期…如何在linq中获取日期。

“System.DateTime”类型的表达式不能用于返回类型“System.Object”

我创建了一个表达式,我正在使用它进行排序工作正常,直到我点击DateTime字段,我得到以下错误(在第二行): “System.DateTime”类型的表达式不能用于返回类型“System.Object” 这是我的代码: ParameterExpression param = Expression.Parameter(typeof(MyEntity), “x”); Expression<Func> sortExpression = Expression.Lambda<Func>( Expression.Property(param, sortKey), param); 任何人都可以帮忙吗?

使用LINQ获取两个列表之间的差异

我有两个列表,我需要比较它们,只返回两个项目列表。 var listOfIds = new List {1,2,4}; var persons = new ObservableCollection { new Person {Id = 1, Name = “Person 1”}, new Person {Id = 2, Name = “Person 2”}, new Person {Id = 3, Name = “Person 3”}, new Person {Id = 4, Name = “Person 4”} }; 在这个例子中, new Person {Id = […]

在LINQ中使用Union合并列表时删除重复项

我试图在LinqPad中使用list.Union合并两个列表,但我无法让它工作,并想检查我的理解是否正确。 鉴于这个简单的类: public class Test { public int Id { get; set;} public int field1 { get; set; } public bool Equals(Test other) { return this.Id.Equals(other.Id); } } 两个列表填充如下: List list = new List(); list.Add( new Test { Id = 1, field1 = 1}); list.Add( new Test { Id = 1, field1 = 2}); list.Add( […]