linq-to-objects

Using Generics with LinqToObjects and NOT IN

I want to turn the follow function into a Lamda. After working on it for 45 minutes, I decided to go old school. How would one do this with a Lamda? public static void NotIn<T>(List<T> inListOne, List<T> notInListTwo,ref List<T> resultList) { resultList = new List<T>(); foreach (T item in inListOne) { if (notInListTw...

Can I use LINQ to strip repeating spaces from a string?

A quick brain teaser: given a string This is a string with repeating spaces What would be the LINQ expressing to end up with This is a string with repeating spaces Thanks! For reference, here's one non-LINQ way: private static IEnumerable<char> RemoveRepeatingSpaces(IEnumerable<char> text) { bool isSpace = false; foreach ...

Text file , CVS xml or what are my options ?

I want to develop an app for Windows 7 phone. It will be a GRE vocab app. i dont want to use SQL CE, my app will have 3 tables conceptually. I will be using Linq to SQL to querying data. Now i am confuse how should i store data so it is easy for retrival and update. XML , CSV , XML or any other format ? ...

Unable to get field values using Dynamic linQ in AsQueryable.

var emp = from c in root.Descendants("Objects") // From Xml XDocument select new { ObjType = (string)c.Element("ObjectType").Value, //Employee object ID = (string)c.Element("ObjectID").Attribute("ID").Value ...

Casting a list of lists to IGrouping?

I have some code that creates a list of lists. The list is of this type: List<List<Device>> GroupedDeviceList = new List<List<Device>>(); But need to return the result in the following type: IEnumerable<IGrouping<object, Device>> Is this possible via a cast etc or should I be using a different definition for my list of lists? ...

Why is this LINQ Where clause not returning results?

We have an entity with DateTime property DateDestroyed. A query needs to return results where this value is between nullable DateTimes startDate and endDate. The where clauses I have are: .Where(x => startDate.HasValue ? startDate <= x.DateDestroyed : true) .Where(x => endDate.HasValue ? x.DateDestroyed <= endDate : true); The query ...

How can I clean up this LINQ Query (SelectMany) ?

How can I clean up this LINQ Query to use SelectMany in the sql syntax, instead of method chaining at the end like I have done? var runPeakWidths = (from ipa in runAnalysis.PassAnalyses let peakWidths = BuildPeakWidths(ipa) select peakWidths) .SelectMany(data => data); Edit: Turned into a tight little method: ...

What's your favorite LINQ to Objects operator which is not built-in?

With extension methods, we can write handy LINQ operators which solve generic problems. I want to hear which methods or overloads you are missing in the System.Linq namespace and how you implemented them. Clean and elegant implementations, maybe using existing methods, are preferred. ...

Linq Puzzle: How do I convert this foreach to linq syntax?

List<string> nameSpaceSuffixes = GetSuffixes(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach(var suffix in nameSpaceSuffixes) { if (assembly.GetName().Name.EndsWith(suffix)) Register(container, assembly, suffix); } } ...

Linq to objects Update

Hi all. 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 thi...

LINQ or foreach - style/readability and speed

I have a piece of code for some validation logic, which in generalized for goes like this: private bool AllItemsAreSatisfactoryV1(IEnumerable<Source> collection) { foreach(var foo in collection) { Target target = SomeFancyLookup(foo); if (!target.Satisfactory) { return false; } } ...

Can I use Expression<Func<T, bool>> and reliably see which properties are referenced in the Func<T, bool>?

I'm writing something in the flavour of Enumerable.Where in that takes a predicate of the form Func<T, bool>. If the underlying T implements INotifyPropertyChanged, I'd like to be a bit more intelligent about re-evaluating the predicate. I'm thinking about changing it to use Expression<Func<T, bool>>, and then using the expression tree ...

Why does LINQ query throw an exception when I attempt to get a count of a type

public readonly IEnumerable<string> PeriodToSelect = new string[] { "MONTH" }; var dataCollection = from p in somedata from h in p.somemoredate where h.Year > (DateTime.Now.Year - 2) where PeriodToSelect.Contains(h.TimePeriod) select new { p.Currency, h.Year.Month, h.Value ...

LINQ Find non overlapping audit records by date

I have an audit table which stores audit rows for a long running process at an individual task level in SQL Server. auditId int, TaskName nvarchar(255), StartTime datetime, EndTime (datetime, null), Status (nvarchar(255), null) The process contains parent and child steps and each step is logged to the audit table. A task may or ma...

LINQ to Object comparing two lists of integer for different values

I accept both C# and VB.NET suggestion, even though I'm writing an app in VB.NET I have two lists of intergers List1 {1,2,3,5} List2 {2,4,6,7} I want to have new List3 {4,6,7} which is composed of elements of List2 that are not in List1. I know I can write a nice For Each loop for this but I want it done in LINQ I've been lookin...

AsParallel: does it make sense for small number of elements in container?

public class ReplacementService : IReplacementService { public List<int> GetTeamReplacementWeight(int iTeamId) { IEnumerable<TeamReplacementGroup> groups = TeamReplacementGroup.GetTeamGroups(iTeamId); List<int> weights= groups .Select(group => group.GetWeight()) .AsParallel() .T...

Translate Obscure LINQ

Some long-gone developer left the following LINQ query behind with no documentation and I'm struggling to understand what it does (and therefore if it's doing it right). Can someone help translate this, either by breaking it into pieces or providing the SQL equivalent? Dim matches = From mc In mcs _ Join ri In r.Items On ...

LINQ Conditional Grouping

Hi I have a problem trying to figure out a LINQ query for the following. The columns are MeterSerialNumber, Date, DeviceType (M or C), and then 48 reading value columns. Some meters will have a corrector fitted. For these meters there will be both an M (DeviceType) row and a C row for the same date. I need just the C rows for these me...

Using SelectMany Extension Method to Select Multiple Columns from DataRow List with result as subset of datarow list

I have a datatable containing 10 columns. I want to select only two columns of them. I am not able to do it using SelectMany Extension Method. I know how to get it from Linq To DataSet but trying using this extension method. ...

Linq to XML to Object

I have an XML in this format which is mapped to corresponding Objects in VB.NET. <Control Name="LoginPage" LabelAlignment="Right" FieldAlignment="Left" DisplayPoweredByLogo="False"> <Fields> <FieldGroup Name="Admin" Visible="True" LayoutPosition="Left"> <AdminID Visible="False" Required="False" EnableChange="False" Max...