linq-to-objects

Linq to objects - select first object

I know almost nothing about linq. I'm doing this: var apps = from app in Process.GetProcesses() where app.ProcessName.Contains( "MyAppName" ) && app.MainWindowHandle != IntPtr.Zero select app; Which gets me all the running processes which match that criteria. But I don't know how to get the first one. The examples I can find...

Learning about LINQ

Overview One of the things I've asked a lot about on this site is LINQ. The questions I've asked have been wide and varied and often don't have much context behind them. So in an attempt to consolidate the knowledge I've acquired on Linq I'm posting this question with a view to maintaining and updating it with additional information as ...

How to find an implementation of a C# interface in the current assembly with a specific name?

I have an Interface called IStep that can do some computation (See "Execution in the Kingdom of Nouns"). At runtime, I want to select the appropriate implementation by class name. // use like this: IStep step = GetStep(sName); ...

Filter linq list on property value

I have a List<int> and a List<customObject>. The customObject class has an ID property. How can I get a List<customObject> containing only the objects where the ID property is in the List<int> using LINQ? Edit: I accepted Konrads answer because it is easier/more intuitive to read. ...

Dynamic LINQ OrderBy

I found an example in the VS2008 Examples for Dynamic LINQ that allows you to use a sql-like string (e.g. OrderBy("Name, Age DESC")) for ordering. Unfortunately, the method included only works on IQueryable<T>. Is there any way to get this functionality on IEnumerable<T>? ...

IList<T> to IQueryable<T>

I have an List and I'd like to wrap it into an IQueryable. Is this possible? ...

IQueriable<T> for objects with better than O(n) performance?

Are there any IQueriable implementaions for linq-to-objects that perform better than the default O(n) linear search performance that you get when calling myEnumerable.AsQueriable()? I've had a look at http://www.codeplex.com/i4o/ which has better performance, but seems to rely on using extension methods on IndexedCollection rather than ...

How to debug a LINQ Statement

I have a Linq to objects statement var confirm = from l in lines.Lines where (l.LineNumber == startline.LineNumber) || (l.LineNumber == endline.LineNumber) select l; The confirm object is returning an 'Object Null or Not A Reference' at at System.Linq.Enumerable.WhereListIterator`1.MoveNext() If the result of the query was empty...

LINQ query with multiple aggregates

How would I create the equivalent Linq To Objects query? SELECT MIN(CASE WHEN p.type = "In" THEN p.PunchTime ELSE NULL END ) AS EarliestIn, MAX(CASE WHEN p.type = "Out" THEN p.PunchTime ELSE NULL END ) AS LatestOUt FROM Punches p ...

How do I get the max data size of every column in an IQuerable using LINQ

I have a method that takes an IQueryable. Is there a LINQ query that will give me back the size of each column in the IQuerable? To be more clear: this is Linq-to-objects. I want to get the length of the ToString() of each "column". ...

Is there a linq-y way to union collection properties of a collection of objects?

Sorry, that title just hurts. I'm wondering if there is a Linq to collections extension method that collapses the following code segment into a single line: public IEnumerable<Child> GetAllChildren(IEnumerable<Parent> parents){ var result = new List<Child>(); foreach(Parent parent in parents) foreach(Child child in parent.Chi...

Linq to XML 'Where not in' syntax problem

The following Code does not compile Dim BasicGroups As String() = New String() {"Node1", "Node2"} Dim NodesToRemove = From Element In SchemaDoc.Root.<Group> _ Where Element.@Name not in BasicGroups For Each XNode In NodesToRemove XNode.Remove() Next It is supposed to Remove any Immediate child of the rootnode w...

Checking for duplicates in a complex object using Linq or Lamda expression

I've just started learning linq and lamda expressions, and they seem to be a good fit for finding duplicates in a complex object collection, but I'm getting a little confused and hope someone can help put me back on the path to happy coding. My object is structured like list.list.uniqueCustomerIdentifier I need to ensure there are no d...

Challenge: elegantly LINQify this procedural code

string[] filesOfType1 = GetFileList1(); string[] filesOfType2 = GetFileList2(); var cookieMap = new Dictionary<string, CookieContainer>(); Action<string, Func<string, KeyValuePair<string, CookieContainer>>> addToMap = (filename, pairGetter) => { KeyValuePair<string, CookieContainer> cookiePair; try { cookiePair = pairGetter(fi...

"Join" of time series

I am designing a simple internal framework for handling time series data. Given that LINQ is my current toy hammer, I want to hit everything with it. I want to implement methods in class TimeSeries (Select(), Where() and so on) so that I can use LINQ syntax to handle time series data Some things are straight forward, e.g. (from x in A ...

Deleting items from one collection in another collection

I've got two collections (generic Lists), let's call them ListA and ListB. In ListA I've got a few items of type A. In ListB I've got some items of type B that have the SAME ID (but not same type) as the items in ListA, plus many more. I want to remove all the items from ListB that have the same ID as the ones in ListA. What's the best ...

Retrieve an object that has a member variable with the longest string length

Given a Generic List of objects that contain a member variable that is a string, what is the best way to get the object that contains the string with the longest length? ie. assuming val1 is the string I'm comparing: 0 : { val1 = "a" } 1 : { val1 = "aa" } 2 : { val1 = "aba" } 3 : { val1 = "c" } what needs to be returned is objec...

Recursive control search with Linq

If I wanted to find checked check boxes on an ASP.NET page I could use the following Linq. var checkBoxes = this.Controls .OfType<CheckBox>() .TakeWhile<CheckBox>(cb => cb.Checked); That works fine if the checkboxes are nested in the current control collection, but I'd like to know how to exte...

Having some confusion with LINQ

Some background info; LanguageResource is the base class LanguageTranslatorResource and LanguageEditorResource inherit from LanguageResource LanguageEditorResource defines an IsDirty property LanguageResourceCollection is a collection of LanguageResource LanguageResourceCollection internally holds LanguageResources in Dictionary<string...

Better performance on updating objects with linq

I have two lists of custom objects and want to update a field for all objects in one list if there is an object in the other list which matches on another pair of fields. This code explains the problem better and produces the results I want. However for larger lists 20k, and a 20k list with matching objects, this takes a considerable ti...