linq

Which method performs better: .Any() vs .Count() > 0?

Hi folks, in the System.Linq namespace, we can now extend our IEnumerable's to have theAny() and Count() extension methods. I was told recently that if i want to check that a collection contains 1 or more items inside it, I should use the .Any() extension method instead of the .Count() > 0 extension method because the .Count() extensio...

Locating the getter and setter access modifiers in the EdmItemCollection of Entity Framework

I've been creating a nice T4 template for a repository pattern for my entities. Instead of manually parsing the xml in the edmx file I use the EdmItemCollection to create a object graph presentation for the conceptual model. I've been able to get a lot of information out of this model. But I cant find where to locate the Getter and Sett...

Using LINQ to get column value from column name when you have the row?

I'm trying to convert some code that uses datasets to LINQ. Some of the code passes column names into other functions as strings. Is there anyway I can easily rewrite this into LINQ? string s = getElement(tr, elementName); private string getElement (tableRow re, string elementName){ if(tr[elementName] != null){ return tr[...

How to lazy-load a single property on a Linq entity?

I'm hand-coding Linq to SQL Entities and have a large XML column that I don't want loaded each time this entity is loaded, as it's infrequently used. Everywhere I search, I see that the Linq to SQL designer provides a "Delay Loaded" property, but the ColumnAttribute class doesn't have any hints as to how this is implemented. I've gather...

c# XML manipulation VB code conversion query... and more!

I am following a VB tutorial to do some HTML manipulation using LINQ It has the following construct Imports <xmlns="http://www.w3.org/1999/xhtml"&gt; How do I do the same in C#? There appears to be something called an XMLNamespaceManager that may hold the solution, but I am too foolish to understand how to work it, and I am not sur...

How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?

public static IQueryable<TResult> ApplySortFilter<T, TResult>(this IQueryable<T> query, string columnName) where T : EntityObject { var param = Expression.Parameter(typeof(T), "o"); var body = Expression.PropertyOrField(param,columnName); va...

Rollback a stored procedure call from inside a transaction using LINQ-to-SQL?

I have a C#.net winform program which runs with a SQL Server database. I am using LINQ-to-SQL. Is it possible to rollback the call to one or more stored procedures inside a transaction within my program using LINQ-to-SQL? Initially I thought it would make sense to manage the transaction inside the stored procedure but if I need to r...

Advice on the best way to structure/format a LINQ to XML query?

I have written a LINQ to XML query that does what I want, but it looks pretty ugly. I am wondering, how would you guys format the following query in a way that doesn't look quite so garish? Apologies if my example is a bit verbose. The XML documents I am querying have the following structure: <?xml version="1.0" encoding="iso-8859-1"...

How to Query A DataGridView Using Linq

I have a DataGridView that I want to query using Linq (C# WinForm). I want to "count" rows where a certain criteria is met. For example, variable1 = "count rows where ColumnBoxAge > 3 || < 5" label1.Text = variable1 How to do this in C# WinForm using Linq? ...

How to use Linq to set attributes based on counter

Say I have a xml document that looks like this <foo> <bar id="9" /> <bar id="4" /> <bar id="3" /> </foo> I would like to use linq to reset the id's to 0, 1 ,2. What would be the easiest way to do this? Thanks ...

Programmatically generating typed DataSet for LINQ - "Metadata file" missing?

Title says what i'm trying to do. I can successfully generate an assembly if i don't specify the LinqOverTypedDatasets option, but i want my typed DataSet to support queries with LINQ. My code outputs the error: error CS0006: Metadata file 'System.Data.DataSetExtensions.dll' could not be found The code: //System.Data.DataSet myDataS...

Storing images with LINQ to SQL: Converting byte array or stream to Binary

I am working with LINQ to SQL and my image fields are treated as Binary. It's no big issue to convert the Binary type to byte[] (you can just use the ToArray() method of the Binary object) when I need to render the images, but can someone tell me how to turn either a byte[] or Stream object into the Binary object so I can save it back t...

DataGridView Filtering OnClick Event (C# WinForm)

How to filter my datagridview by the value of my label.text on click event? That value is from my linq query: dataSet.Tables[0].AsEnumerable().Where(c => c.Field<int>("ageColumn") > 3 && c.Field<int>("ageColumn") < 5).Count(); Let's just say the above query gives me 12 (label.text = 12), now when I click "12", I want my datagridv...

How do I sum a list<> of arrays

I have a List< int[] > myList, where I know that all the int[] arrays are the same length - for the sake of argument, let us say I have 500 arrays, each is 2048 elements long. I'd like to sum all 500 of these arrays, to give me a single array, 2048 elements long, where each element is the sum of all the same positions in all the other a...

LINQy way to check if any objects in a collection have the same property value

I have a class Agent with a property Id Given a collection of Agents I need to check if any of them have duplicate Ids. I am currently doing this with a hash table but am trying to get Linq-ified, what's a good way of doing this? ...

Can you use LINQ or lambdas to perform matrix operations?

I know how to do this using for loops. Is it possible to do something like this using LINQ or lambdas? int[] a = { 10, 20, 30 }; int[] b = { 2, 4, 10 }; int[] c = a * b; //resulting array should be { 20, 80, 300 } ...

De/Serialize directly To/From XML Linq

Is there any way to de/serialize an object without round-tripping a XmlDocument/temp string? I am looking for something like the following: class Program { static void Main(string[] args) { XDocument doc = new XDocument(); MyClass c = new MyClass(); c.SomeValue = "bar"; doc.Add(c); Conso...

Something better than .ToArray() to force enumeration of LINQ output

I'm working with LINQ to objects and have a function where in some cases I need to modify the underlying collection before calling Aggregate(...) and then return it to its original state before the funciton returns the results of Aggregate(...). My current code looks something like this: bool collectionModified = false; if(collectionNee...

C# LINQ SEO General programming question, how to correctly get the results of a location?

Hi guys, sorry about the title :) Here is my basic problem, I trying to implement an SEO type query for a location. Here are my examples /Leeds /Leeds_England /Hampshire_England /England_Leeds /Europe_England I am trying to get the location, now I am splitting on the '_', then doing a LINQ lookup through my List's for each part. Lo...

How do I get the max ID with Linq to Entity?

I have a table User which has an identity column UserID, now what is the correct Linq to Entity line of code that would return me the max UserID? I've tried using (MyDBEntities db = new MyDBEntities()) { var User = db.Users.Last(); // or var User = db.Users.Max(); return ...