linq

Hierarchical queries in LINQ

Here is a simple LINQ query (based on NorthWind) that returns a list of Customers. Each customer contains a list of Orders. from c in Customers join o in Orders on c.CustomerID equals o.CustomerID into CO select new {c, CO} This works fine and the SQL generated is also fine. Now I want to go one step further. I want each Order object ...

Should I use LINQ for this query?

Hi guys, I've been reading a fair bit about the performance of using LINQ rather than using a for each loop and from what I understand using a LINQ query would be a little bit slower but generally worth it for convenience and expressiveness. However I am a bit confused about how much slower it is if you were to use the results of the q...

Was is the performance of the Last() extension method for List<T>?

I really like Last() and would use it all the time for List<T>s. But since it seems to be defined for IEnumerable<T>, I guess it enumerates the enumeration first - this should be O(n) as opposed to O(1) for directly indexing the last element of a List<T>. Are the standard (Linq) extension methods aware of this? The STL in C++ is aware...

Linq and nullable varchar column

Hello, is there any posibillity to turn off Linq replacing empty strings with null on nullable varchar columns? I could set the column to NOT NULL, but the scheme is used by other application too, so I would like to avoid that. I've tried to set column not nullable in DBML, but then I get "System.Data.Linq.ChangeConflictException: Raw...

Convert linq query to string array - C#

What is the most efficient way of converting a single column linq query to a string array? private string[] WordList() { DataContext db = new DataContext(); var list = from x in db.Words orderby x.Word ascending select new { x.Word }; // return string array here } ...

Linq with DbNull values

I have a DataTable that contains columns that would contain DbNull values. I want to use Linq and output a new DataTable but I want the DbNull values to be perserved. Here is an example: DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("C1",System.Type.GetType("System.String"))); dt.Columns.Add(new...

linq puzzle ... equivalent syntax ... not equivalent results!!

In the code there exists exactly one type that implements IResourceConverter. That's what the two following linq statements are looking for. The former does not find it. The latter does. However they both are equivalent syntaxes (or at least should be!). Linq Statement 1: List<Type> toInstantiate = AppDomain.CurrentDomain.GetAssemb...

Linq and multiple Where's

I'm trying to see if there is a way to build a Linq statement that would choose based off available attributes in an element what the result would be, although not all attributes are always available. For example, this would be a 'standard' element: <box left="2" right="2" /> However, this is also perfectly valid: <box left="3" /> ...

LINQ to SQL Math.Round Problem

Hello all, I'm having an issue with a query I wrote where for some reason the variable that I'm using to store a decimal value in returns 6 values after the decimal place (they're mostly 0). I have tried the following (and different combinations using Math.Round) with no luck. Sales = (from invhed in INVHEAD ......

C# linq sort - quick way of instantiating IComparer

When using linq and you have c.Sort() Is there any good inline way of defining a Comparison and/or IComparer class without actually having to create a separate class? ...

Unexpected results in Linq query

I have a Linq query that looks something like this: var myPosse = from p1 in people select p1; label1.Text = "All my peeps:" + Environment.NewLine; foreach (Person p in myPosse) { this.label1.Text += p.ToString() + Environment.NewLine; } This gives me good results. But when I do something like this: var myPosse = fr...

Why I see so much code duplication in Linq in Reflector?

Hi! Edit: I made a mistake in my original question. It should be about methods Last and LastOrDefault (or Single and SingleOrDefault, or First and FirstOrDefault - plenty of them!). Inspired by this question, I opened Reflector and looked at code of Enumerable.Last<T>(this collection) Then I jumped to code of Enumerable.LastOrDefau...

Linq to SQL, SQL Server 2008 and optimization

I'm designing a large scale web application with about 30 tables, more than 2/3 of which are related to each other. I'm using ASP.NET MVC, Linq to SQL with SQL Server 2008. These tables are meant to hold thousands of records. As a programmer, what should I focus on to help optimize the database and the queries to and from Linq? Do you...

In continue of questions "DB Schema for storing tagged records" -- how to select list of items with tags?

Good day! There are a lot of questions how to store tags in DB here on stackoverflow, I finally decide to use Toxi approach (items table, tags table and many-to-many intermediate table) http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html I want to display a list of 20-50 tagged items on a page each with list of it's tag...

Periodic InvalidCastException and "The server failed to resume the transaction" with Linq

I see this on our stage system, after it has been up for 2-3 days. "The server failed to resume the transaction. Desc:39000000ef." (with desc:xxx increasing every time). The stack trace shows System.Data.SqlClient.SqlException: The server failed to resume the transaction. Desc:39000000ef. at System.Data.SqlClient.SqlConnection.OnErro...

Populating Html.DropDownList with foreign key table vlaues

Hello everybody! I am facing the problem that I cannot properly map my foreign key table values to an asp.net mvc edit view. I have placed a Html.DropDownList and made an IENumerable/IQueryable helper function returning: from item in db.items select item.name which populates my Html.DropDownList with all the Names in the foreign ke...

How to relate Linq-To-Sql objects that are on different dbml diagrams?

I have two different dbml diagrams reflecting my Linq-To-SQL classes. (This is necessary, because they appear in different projects.) One of the objects in the one diagram needs an association with an object in the other diagram. How do I do it? ...

Cannot .Count() on IQueryable (NHibernate)

Hello, I'm with an irritating problem. It might be something stupid, but I couldn't find out. I'm using Linq to NHibernate, and I would like to count how many items are there in a repository. Here is a very simplified definition of my repository, with the code that matters: public class Repository { private ISession session; /*...

System.Linq.Dynamic and DateTime

I am using System.Linq.Dynamic to do custom where clauses from an ajax call in .Net MVC 1.0. It works fine for strings, int etc but not for DateTime, I get the exception cannot compare String to DateTime. The very simple test code is items = items.Where(string.Format(@" {0} > {1}{2}{1} ", searchField, delimiter, searchString)); Where...

Automapper or Otis: Mapping ReadonlyDictionary <name, value> to a DTO's IList<string>

I am trying out Automapper as well as otis. I have a source type that is something list this (only partial code shown here): class Source { ReadOnlyDictionary<string, customtype> items; } My DTO is like so: class ClassDTO { IList<string> pageNames; } Now how do I map the pagenames in my DTO to "items" in the source type, ...