linq-to-objects

why ForEach Linq Extension on List rather than on IEnumerable [duplicate]

Deadly 提交于 2019-12-12 08:27:34
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface? Hello, My question is why Foreach extension method is defined on List rather than IEnumreable. i have read Eric Lippert's article but the point is, if it is so bad to have such method than why is it there for List? 回答1: List<T>.ForEach() isn't an extension method. It's just a method on List<T> . One of the main reasons that it isn't

Why does using anonymous type work and using an explicit type not in a GroupBy?

为君一笑 提交于 2019-12-12 07:13:43
问题 I have a problem where I want a group type to be strongly typed but if I do it doesn't group correctly. See the code below... using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication35 { class Program { static void Main(string[] args) { List<Foo> foos = new List<Foo>(); foos.Add(new Foo() { Key = "Test" }); foos.Add(new Foo() { Key = "Test" }); foos.Add(new Foo() { Key = "Test" }); var groups = foos.GroupBy<Foo, dynamic>(entry => new { GroupKey = entry

How to write Linq left join with multiple fields where one is case insensitive

旧时模样 提交于 2019-12-12 04:37:49
问题 What is preferred way to convert this to a Left Join? This is Linq to Objects (not SQL). var result = ( from e in entries from r in results where r.PropertyId == e.PropertyId && e.ValueAsString.Equals(r.Value, StringComparison.InvariantCultureIgnoreCase) select new { Result = r, Entry = e } ) .ToList(); Issues: Tradeoff between readability vs. efficiency. Should try to avoid ToLowerInvariant()? 回答1: For left joins you need to use DefaultIfEmpty var result = ( from e in entries from r in

Is there an application for displaying some kind of query plan for a Linq to object query?

泪湿孤枕 提交于 2019-12-12 04:31:05
问题 I'm looking for an application to display what a linq expression would do, in particular regarding the usage of multiple access to the same list in a query. Better yet, the tool would tell if the linq query is good. 回答1: I used the expression tree visualizer in the past to at least help decode what is inside of an expression tree. It aids in figureing out the parts of the tree and how gives each part is related. 回答2: Well, to begin with, I could easily foresee a tool that would pick a query

Linq query to group by field1, count field2 and filter by count between values of joined collection

北战南征 提交于 2019-12-12 01:59:09
问题 I'm having trouble with getting a my linq query correct. I've been resisting doing this with foreach loops because I'm trying to better understand linq. I have following data in LinqPad. void Main() { var events = new[] { new {ID = 1, EventLevel = 1, PatientID = "1", CodeID = "2", Occurences = 0 }, new {ID = 2, EventLevel = 2, PatientID = "1", CodeID = "2", Occurences = 0 }, new {ID = 3, EventLevel = 1, PatientID = "2", CodeID = "1", Occurences = 0 }, new {ID = 4, EventLevel = 3, PatientID =

Linq - how to loop a tree data structure to build a tree style object

冷暖自知 提交于 2019-12-12 01:55:42
问题 I have the following SQL table data: The visual tree should look something like this: To get the very top nodes I'm using: var parentNodes = data .Where(i => i.AncestorId == i.DescedantId && (data.Count(d => d.DescedantId == i.DescedantId) == 1)) .ToList(); Any clue on how to build a function that would loop trough the structure and then build the tree style object? My tree style object classes are: public class ProfitCenterRoot { public List<ProfitCenterItem> Data { get; set; } } public

Do at compile time Linq-To-Objects statements also get translated into Expression Tree objects?

我的梦境 提交于 2019-12-12 01:52:20
问题 At compile time LINQ statements that operate on IQueryable<T> ( thus Linq-to-SQL and Linq-to-Entities statements ) get translated into an expression tree objects that present code as data. a) Do LINQ statements that operate on IEnumerable<T> ( thus LINQ-to-Objects ) also get translated into expression trees? b) If not, what happens with LINQ-to-Object statements at compile time? Does compiler simply translate them into appropriate method calls? For example, is the next Linq-to-Objects

Get Directories with Parallel ForEach

自闭症网瘾萝莉.ら 提交于 2019-12-11 18:48:11
问题 I've got a method that recurses over a directory and builds a Tree: public void RecurseFolders(TreeNode mainNode) { DirectoryInfo nodeDir = new DirectoryInfo(mainNode.Tag.ToString()); try { foreach (var dir in nodeDir.GetDirectories()) { int index = GetSystemIcon(dir.FullName, treeView1.ImageList, false); var subNode = new TreeNode(dir.Name, index, index); subNode.Tag = dir.FullName; mainNode.Nodes.Add(subNode); RecurseFolders(subNode); } } catch (UnauthorizedAccessException err) { Console

Linq to objects Update

百般思念 提交于 2019-12-11 11:46:42
问题 Im using Linq - objects and I need to do an update. I've been looked around for a while but not really found anything that matches. So for arguments sake I have 2 simple List's, both with a key so I can use join happily. I cant find a simple way to update results from obj2 into obj1. for simple updates I'm currently doing this: string newValue = "something"; var items = obj1.Where(w => w.ID == iKey).Select(c => {c.<property> = newValue; return c; }).ToArray(); And that all works well and

Exclude directories that contain sub directories?

三世轮回 提交于 2019-12-11 11:01:33
问题 I have the following code: var directories = Directory.GetDirectories( environmentSettings.SourcePath, "*", SearchOption.AllDirectories) .Where(dir => !environmentSettings.FolderExclusions .Contains(Path.GetFileName(dir))); I want to create a directory in the target path except for ones that show up in the exclusion list. This works, but only if the directory is directly under the root and does not contain sub-directories. As an example, if the exclusion list contains a directory called