Now I come a stage to get all my data as a list in cache(objects) and my next thing I have to do is to remove some instances from the list. Normally, I would do removing like this: List<T> list; List<T2> toBeRemovedItems; // populate two lists foreach(T2 item in toBeRemovedItems) { list.Remove(delegate(T one) { // build a condition based on item // return true
现在我来了一个阶段,将所有数据作为缓存(对象)中的一个列表,我要做的下一件事就是从列表中删除一些实例。 通常情况下,我会这样删除: List<T> list; List<T2> toBeRemovedItems; // populate two lists foreach(T2 item in toBeRemovedItems) { list.Remove(delegate(T one) { // build a condition based on item // return true or false }); } 更具体地说,我实际上构建或填充
I have used .Net 3.5 and VS 2008 for more than a month. Like most .Net developers, I have evolved from years experience in .Net 1.0 & 2.0 and VS 2005. Just recently, I discovered the simplicity and power of LINQ and Lambda Expressions, as in my recent questions such as Find an item in list by LINQ, Convert or map a class instance to a list of another one by using Lambda or LINQ, and Convert
我已经使用.Net 3.5和VS 2008超过一个月。 像大多数.Net开发人员一样,我已经从.Net 1.0&2.0和VS 2005的多年经验发展而来。就在最近,我发现了LINQ和Lambda表达式的简单性和强大功能,就像我最近的问题,例如在列表中查找项目LINQ,使用Lambda或LINQ将类实例转换或映射到另一个类的实例列表,以及使用Lambda或LINQ将类的列表转换或映射到另一个类的列表。 我承认Lambda和LINQ更简单易读,而且看起来非常强大。 在幕后,.Net
I have a collection of MyClass that I'd like to query using LINQ to get distinct values, and get back a Dictionary<string, string> as the result, but I can't figure out how I can do it any simpler than I'm doing below. What would some cleaner code be that I can use to get the Dictionary<string, string> as my result? var desiredResults = new Dictionary<string, string&g
我有一个MyClass的集合,我希望使用LINQ来查询以获取不同的值,然后返回一个Dictionary <string,string>作为结果,但我无法弄清楚如何做到这一点比我更简单在下面做。 我可以使用哪些更干净的代码来获得Dictionary <string,string>作为我的结果? var desiredResults = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); var queryResults = (from MyClass mc in myClassCollection
I have an object " MyObject " with the properties (all string): " PropA ", " PropB " and " PropC ". var List = new List(); I add some object in this list with the following value : List.Add(new MyObject { PropA = "AA", PropB = "1", PropC = "TT"}); List.Add(new MyObject { PropA = "AA", PropB = "1", PropC = "TT"}); List.Add(new MyObject { PropA = "AA", P
我有一个对象“ MyObject ”与属性(所有字符串):“ PropA ”,“ PropB ”和“ PropC ”。 var List = new List(); 我用下面的值在这个列表中添加一些对象: List.Add(new MyObject { PropA = "AA", PropB = "1", PropC = "TT"}); List.Add(new MyObject { PropA = "AA", PropB = "1", PropC = "TT"}); List.Add(new MyObject { PropA = "AA", PropB = "1", PropC = "TT"}); List.Add(new MyObject { PropA = "BB", PropB = "1
I have a list of objects that contain a Person object which may have a null. What I'd like to do is get the value of the Name property of the first Person object that is not null and if all Person objects are null, return an empty string. My best attempt is as follows: string userName = MyObjectList.FirstOrDefault(x => x.Person != null).Person.Name ?? string.Empty; I think I under
我有一个包含可能有空的Person对象的对象列表。 我想要做的是获取第一个Person对象的Name属性的值不为null,如果所有Person对象都为null,则返回一个空字符串。 我最好的尝试如下: string userName = MyObjectList.FirstOrDefault(x => x.Person != null).Person.Name ?? string.Empty; 我想我明白为什么这不起作用; 如果Person对于列表中的每个对象都为null,那么我们将得到默认值,该值将为null,并在尝试访问Pe
Is there a way to do the following using LINQ? foreach (var c in collection) { c.PropertyToSet = value; } To clarify, I want to iterate through each object in a collection and then update a property on each object. My use case is I have a bunch of comments on a blog post, and I want to iterate through each comment on a blog post and set the datetime on the blog post to be +10 hours. I co
有没有办法使用LINQ执行以下操作? foreach (var c in collection) { c.PropertyToSet = value; } 为了澄清,我想迭代集合中的每个对象,然后更新每个对象的属性。 我的使用案例是,我在博客文章中有大量评论,并且我想遍历博客帖子上的每条评论,并将博客帖子的日期时间设置为+10小时。 我可以在SQL中完成,但我想保留在业务层中。 虽然您可以使用ForEach扩展方法,但如果您只想使用您可以执行的框架 collection.Sel
This question already has an answer here: How to use LINQ to select object with minimum or maximum property value 12 answers Have a look at the MinBy extension method in MoreLINQ (created by Jon Skeet, now principally maintained by Atif Aziz). MinBy documentation MinBy source code (it's pretty straightforward and has no dependencies on other files). Use Aggregate: items.Aggregate((
这个问题在这里已经有了答案: 如何使用LINQ来选择具有最小或最大属性值的对象12个答案 查看MoreLINQ中的MinBy扩展方法(由Jon Skeet创建,现在主要由Atif Aziz维护)。 MinBy文档 MinBy源代码(它非常简单,并且不依赖于其他文件)。 使用聚合: items.Aggregate((c, d) => c.Score < d.Score ? c : d) 如所建议的,与更友好的名字完全相同 : items.Aggregate((minItem, nextItem) => minItem.Score <
This question already has an answer here: How do you give a C# Auto-Property a default value? 21 answers Update - the answer below was written before C# 6 came along. In C# 6 you can write: public class Foo { public string Bar { get; set; } = "bar"; } You can also write read-only automatically-implemented properties, which are only writable in the constructor (but can also be given a
这个问题在这里已经有了答案: 你如何给C#自动属性一个默认值? 21个答案 更新 - 下面的答案是在C#6出现之前编写的。 在C#6中,您可以编写: public class Foo { public string Bar { get; set; } = "bar"; } 您也可以编写只读的自动实现的属性,这些属性只能在构造函数中写入(但也可以赋予默认的初始值: public class Foo { public string Bar { get; } public Foo(string bar) { Bar =
Note: This was posted when I was starting out C#. With 2014 knowledge, I can truly say that auto-properties are among the best things that ever happened to the C# language. I am used to create my properties in C# using a private and a public field: private string title; public string Title { get { return title; } set { title = value; } } Now, with .NET 3.0, we got auto-properties:
注意:这是在我开始使用C#时发布的。 凭借2014年的知识,我可以真正地说,自动属性是C#语言发生过的最好的事情之一。 我习惯使用私有和公共字段在C#中创建我的属性: private string title; public string Title { get { return title; } set { title = value; } } 现在,在.NET 3.0中,我们获得了自动属性: public string Title { get; set; } 我知道这更多是一个哲学/主观的问题,但是有没有任何理由使用
There's a lot of advice out there that you shouldn't expose your fields publically, and instead use trivial properties. I see it over & over. I understand the arguments, but I don't think it's good advice in most cases. Does anyone have an example of a time when it really mattered? When writing a trivial property made something important possible in the future (or when f
在那里有很多建议,你不应该公开地暴露你的领域,而是使用普通的属性。 我一遍又一遍地看到它。 我理解这些论点,但我认为在大多数情况下这不是好建议。 有没有人有一个真正重要的时间的例子? 当写一个微不足道的财产在将来有可能变得重要时(或者当失败时会让他们陷入真正的麻烦)? 编辑:DataBinding参数是正确的,但不是很有趣。 这是DataBinding代码中的一个错误,它不会接受公共字段。 因此,我们必须编写属性