linq-to-objects

Create a list of one object type from a list of another using Linq

If I have classes of Type A and B: public class A { public int TotalCount; public string Title; } public class B { public int Count; public string Title; } I have a list of instances A instances, what is the most efficient way to create and populate a List of type B using Linq? ...

Differences between LINQ to Objects and LINQ to SQL queries

I have been using LINQ to query my POCO objects for some time, but I have not yet tried LINQ to SQL. I assume that LINQ to SQL queries are somehow converted to equivalent SQL queries and, given this, I am wondering if that affects the way LINQ to SQL queries are or should be written. Are there any significant differences between LINQ to...

Using LINQ to find a common prefix?

I've got two sequences: IEnumerable<string> x = new[] { "a", "b", "c" }; IEnumerable<string> y = new[] { "a", "b", "d", "e" }; I'd like to find the common prefix of these two sequences (i.e. "a", "b"). Is there a succinct way to do this in LINQ? Bear in mind that these aren't really IEnumerable<string>; they're IEnumerable<PathCompon...

Speeding up lookup of ranges of data from a collection

Say I have a class public class TimestampedTrackId { private readonly int trackId; private readonly DateTime insertTime; public TimestampedTrackId(int trackId, DateTime insertTime) { this.trackId = trackId; this.insertTime = insertTime; } public int TrackId { get { ...

Get parameter from last iteration with LINQ

How to perform this without iteration, with LINQ? string[] flatSource = {"region1", "subregion1", "region2", "sub1", "sub2", "region3", "sub1", "sub2"}; string previousRegion = ""; foreach (var item in flatSource) { if (SomeRegionDictionary.Contains(item)) previousRegion...

How do I write a LINQ query that inverts the grouping of a hierarchical data source?

How would one write a LINQ query which takes a hierarchical source data and transforms it so that the grouping is inverted? Say I have a list of Topic objects each of which contains a collection of Tags which represent meta-data tags on that topic. What I need is to write a LINQ query to basically flip the hierarchy inside out so that I...

If statement or Where extension with a For Each loop?

I was wondering which of the two code samples would be more efficient (or is the difference between the two negligible)? For Each apple in AppleController.GetRedApples().Where(Function(a) PriceController.OnSale(a)) 'do something Next or For Each apple in AppleController.GetRedApples() If PriceController.OnSale(apple) Then ...

Linq Projection

I have an Customer collection. Inside Customer I have another collection called Orders. Order have Name, OrderDate, PurchaseID. How do write a Linq query to give me a new customer collection that only contain Orders that have OrderDate > QueryDate? To aid with the discussion here is the relevant set of the code. I just need to underst...

AlphaNumeric ordering in SQL vs. LINQ and .NET

I encountered an interesting thing today that I have never noticed before. It appears that SQL and LINQ order AlphaNumeric strings differently. Data table contains rows: A G 6 P 1 D J 2 T Z 9 F 0 If I perform an Order By in the SQL, I receive the following results: A D F G J P T Z 0 1 2 6 9 Now consider this LINQ sample: class Progr...

How to identify LINQable parts in pre-.NET 3.5 code?

This is the scenario: I have a huge code-base written in .NET 2.0...and sometime back the migration happened to .NET 3.5. This code-base is both refactored and enhanced as an ongoing maintenance project. I am looking for patterns to identify in code base which are good candidates for LINQ to Objects. I need pointers for comprehensive a...

Getting the sum of a value in Linq

Given the following structure... class Foo { public string Category { get; set; } public string Code { get; set; } public int Quantity { get; set; } } I am trying to use Linq to summarise a list of these objects by Category and Code, so that if I provide the following source... List<Foo> foos = new List<Foo>() { new Foo {Ca...

Can I do this with an IQueryable<T> ?

Hi folks, is it possible to add an extension method to IQueryable<T> and then, for my Linq2Sql or EF or NHibernate or LinqToObjects, define it's functionality / how each repository will impliment this method / translate this method to some sql? For example, imagine I want the following :- var result = (from q in db.Categories() ...

Creating a Dynamic Linq filter over List<T>

Hi. Ok, I asked this question before, but deleted it as the way I went about describing my problem was wrong. Firstly, let me state that Im creating a .NET3.5 Winforms app using C# and Plinqo (Professional Linq to Objects) as my ORM. Here's my situation: I have a DataGridview that is populated from a SortableBindingList<T> - in my cas...

LINQ Bug ? Get a string array from a list of long

I'm getting this Unable to cast object of type 'System.Int64' to type 'System.String'. from following code snippet: IList<long> Ids = new List<long>(); Ids.Add(6962056); Ids.Add(7117210); Ids.Add(13489241); var stringIds = Ids.Cast<string>().ToArray(); and Booooooooooooom .... ideas ? ...

LINQ to SQL and a running total on ordered results

I want to display a customer's accounting history in a DataGridView and I want to have a column that displays the running total for their balance. The old way I did this was by getting the data, looping through the data, and adding rows to the DataGridView one-by-one and calculating the running total at that time. Lame. I would much r...

VB Syntax for LINQ query that creates a new{} set?

I am trying to AVERAGE 6 different fields on a DataTable, but without grouping. Just the AVERAGE. I have found a couple of examples in C#, but can't find any examples for how to do this in VB. Can someone please help me convert the syntax? Here is what I have so far: Dim query = From dtRow In dtIn.AsEnumerable _ Where d...

A better way to do this LINQ query?

I've got some collections of data objects that can't directly be accessed from one another. I imagine the best solution would be to get the database guys to make a query for this, but in the meantime, is there some way to tighten this up? var conflicting = allFoos.Where(foo => foo.ElectronicSerialNumber != 0 ...

How to get unique entries of products in a List<Order> with LINQ

I have a classic Order -> Order Row scenario something like this: public class Order { public int Id { get; set; } public string Name { get; set; } public List<OrderRow> OrderRows { get; set; } } public class OrderRow { public int Id { get; set; } public int ProductId { get; set; } public int Amount { get; set;...

String.StartsWith not working with tilde ("~") characters LINQ to SQL?

For some reason, my call to IEnumerable.Where() using String.StartsWith() appears to be giving different results depending on whether it's being used in LINQ-to-SQL or standard LINQ (-to-objects). If I add in a call to ToList() on what's otherwise the same call, I get different results back: var withToList = MyDataContext.MyEntities.ToL...

How to use If statement during select from Linq to Datasets

I have this LINQ statement Dim Demo = From d In DBDataTable.AsEnumerable _ Select id = d.Field(Of Integer)("id"), _ Colun = d.Field(Of Object) (_column2), _ Col3 = d.Field(Of Object)(_column3), _ Col4 = IIf(_Col4 <> -1, d.Field(Of Object)(_Col4), Nothing) Is there any wa...