linq

LINQ: take a sequence of elements from a collection

Hi there, I have a collection of objects and need to take batches of 100 objects and do some work with them until there are no objects left to process. Instead of looping through each item and grabbing 100 elements then the next hundred etc is there a nicer way of doing it with linq? Many thanks ...

Working with nullable types in Expression Trees

I have an extension method to dynamically filter Linq to Entities results using string values. It works fine until I use it to filter nullable columns. Here's my code: public static IOrderedQueryable<T> OrderingHelperWhere<T>(this IQueryable<T> source, string columnName, object value) { ParameterExpression table = Expression.Paramet...

How can do an invariant .Contains(myQuery) with Linq to Objects?

Hi folks, I'm trying to filter a list of objects using linq. When i filter by a Contains(someSearchQuery) it's like it's being case sensitive .. meaning i miss some results. I have an IList<Foo>'s which has a number of properties, but one is public string MyText { get; set; } Now, i'm trying to return a IQueryable of Foo's where the...

Is there a way to use linq in C# application with .net framework 2?

I need to use linq in C# Windows form application working with .Net Framework 2 not 3.5 . I Know it may be some sort of stupidity to do that. but i need that for some special reasons. Can i import just linq libraries to my project by anyway ? or there is no way except for using 3.5 ? ...

Use LINQ to read all nodes from XML

I have a XML file, like this: <?xml version="1.0" encoding="utf-8" ?> <ROOT> <NAME> ItemName </NAME> <LIST> <ITEM> ListItem1 </ITEM> <ITEM> ListItem2 </ITEM> <ITEM> ListItem3 </ITEM> </LIST> </ROOT> How can I use LINQ to get all the ITEM...

Linq - Is there a IEnumerable.ForEach<T> extension method that I'm missing?

Possible Duplicates: Lambda Expression using Foreach Clause Why is there not a ForEach extension method on the IEnumerable interface? This seems pretty basic. I'm trying to iterate over each object of an IEnumerable. It appears that I would have to cast it to a list first. Is that right? It seems to me there should be an ext...

Best way to write DataTables from a Web Service into XML?

I'm trying to refactor some slow running code which writes XML using nested loops of several datatables. I read that using linq to write the xml would be faster. I'm not well versed in linq, so I was hoping to get some help here. Some things I need to mention is that the current architecture uses a webservice which returns data to us in...

using Linq to break flat table results into object collection

I have a database return result which has flatten results like below. I want to use Linq to break the flat results into primary classes with the items populating the primary class items property collection. public class Result { public string PrimaryKey { get; set; } public string Status { get; set; } public string ItemName { get; s...

LinqToSql Precompiling queries benefit?

I was looking through the sample LINQ queries provided with LINQPad taken from the C# 4.0 in a Nutshell book, and ran across something I have never used in LINQ to SQL... Compiled Queries. Here is the exact exmaple: // LINQ to SQL lets you precompile queries so that you pay the cost of translating // the query from LINQ into SQL only o...

Tradeoffs using NHibernate 3.0 QueryOver or LINQ provider

I have not found a clear comparison of what is supported with the NHibernate 3.0 LINQ Provider compared to using the QueryOver syntax. From the surface, it seems like two large efforts into two very similar things. What are the key trade offs to using each? ...

Using LINQ to create an IEnumerable<> of delta values

I've got a list of timestamps (in ticks), and from this list I'd like to create another one that represents the delta time between entries. Let's just say, for example, that my master timetable looks like this: 10 20 30 50 60 70 What I want back is this: 10 10 20 10 10 What I'm trying to accomplish here is detect that #3 in the ...

Is there a way, using LINQ/EF, to get the top most item in a parent/child hierarchy?

I have a class called Structure: public class Structure { public int StructureId { get; set; } public Structure Parent { get; set; } } As you can see, Structure has a parent Structure. There can be an indefinite number of structures within this hierarchy. Is there any way, using LINQ (with Entity Framework), to get the top-mo...

Grouping lambda.

if I have a structure like this: Batch Amount 76 495.4 76 975.75 76 25 76 442.46 77 1335.12 77 2272.37 77 34.5 77 496.99 77 360 77 13 77 594.6 And I want to get something like Batch Amount 76 1938.61 77 5106.58 How the expression should be? I started with something like: batches.GroupBy(x => new { Batch = x.Ba...

LinqResult.Any() Resulting In NullReferenceException

What could be causing this error: NullReferenceException was unhanded, Object reference not set to an instance of an object. var LinqResult = from a in Db.Table select new {Table = a}; if(LinqResult.Any()) { //Blah blah blah } ...

how to sort a collection based on type in LINQ

Hi, I have a collection like this, Class Base{} Class A : Base {} Class B : Base {} List<Base> collection = new List<Base>(); collection.Add(new A()); collection.Add(new B()); collection.Add(new A()); collection.Add(new A()); collection.Add(new B()); Now I want to sort the collection based on type (A/B). How I can do this? Please he...

Why is this LINQ so much slower than its SQL counterpart?

I have the below LINQ to SQL method that takes an inordinate amount of time to execute yet its SQL counterpart is quite simple and fast. Am I doing something wrong in the LINQ part? I am just trying to return some data to display, read-only, in a Data Grid. I understand that if the tool doesn't fit don't use it and as such I could j...

Select Where Table.Value == Maximum

I have an SQL table with the following design: TableSchedule: Id Description ImportedDate I can import a large number of items and they will all have the same ImportedDate. How can I write a LINQ query that grabs only the entries with the most recent ImportedDate? var ResultSchedule = from a in Db.TableSchedule where a.Impor...

[linq] CRUDing composite object using association properties / LINQ2SQL desginer

First of all some notes: 1. As I realize I'm asking for relatively a lot, I shall offer a (humble 50 rep) bounty ASAP, even if I'll get an answer before doing that. 2. I'm noob at this, so any direction would help. 3. I'd like only Linq2Sql designer or association properties solution, as it seems most elegant. 4. this is a follow-up ques...

Using Custom Extension Methods Inside Linq Query

I have made my custom In extension method as shown below: public static class ExtensionMethods { public static bool In(this string str, IEnumerable<String> list) { foreach (var s in list) { if (s.Equals(str)) return true; } return false; } ...

Is there a secret to using LINQ to SQL to add records when the object has relationships?

I am working in LINQ to SQL, ASP.NET MVC, and C#. I have a repository called genesisRepository to connect to the database. I have a table represented in an object called Stream. It is defined like this: [Table] public class Stream { [HiddenInput(DisplayValue = false)] [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync...