linq

LINQ: custom column names

UPDATE I'm basically binding the query to a WinForms DataGridView. I want the column headers to be appropriate and have spaces when needed. For example, I would want a column header to be First Name instead of FirstName. How do you create your own custom column names in LINQ? For example: Dim query = From u In db.Users _ ...

Introduction to C# list comprehensions

What is a good introduction or tutorial article for learning how to use list comprehensions in C#? ...

Is there any way to use an extension method in an object initializer block in C#

The simple demo below captures what I am trying to do. In the real program, I have to use the object initialiser block since it is reading a list in a LINQ to SQl select expression, and there is a value that that I want to read off the database and store on the object, but the object doesn't have a simple property that I can set for that...

Linq output as an Interface?

Here's the code that I'm attempting to do: public IList<IOperator> GetAll() { using (var c = new MyDataContext()) { return c.Operators.ToList(); } } Operator implements IOperator, but I'm getting the following compilation error: Cannot implicitly convert type 'System.Collections.Generic.List<MyProject.Core...

Linq To SQL caching VS multi-user application.

We develop Win32 application that access to SQL 2005 database through Linq to SQL. The issue is when 2 users access to same record (View and Edit)… User 1 update record (DataContext.SubmitChanges()), User 2 will continue to see old information until he restart application. So, we would like to update context of user 2… The solution that ...

SQL tracing LINQ to Entities

So, I would like to know oh to do a "full" tracing of Linq to Entities? In other words: I already know about the ToTraceString() method, but this only works on an ObjectQuery. I need it to work on on the entire Linq layer... so when I am doing IQueryable "Where" expressions and additional filtering that I can see the entire query, not ...

¿Cómo programar las extensiones Partial que Linq to SQL autogenera?

Hice una clase desde la herramienta Linq to SQL Clasees con VS 2008 SP1 Framework 3.5 SP1, en este caso extendí el Partial partial void UpdateMiTabla(MiTabla instance){ //Logica de negocio // Reglas de validación etc. } Mi problema es que cuando le doy db.SubmitChanges() si va al metodo UpdatemiTabla y hace las validaciones, pe...

Nullable entity projection in Entity Framework

I have a following SQL Server 2005 database schema: CREATE TABLE Messages ( MessageID int, Subject varchar(500), Text varchar(max) NULL, UserID NULL ) The column "UserID" - which can be null - is a foreign key and links to the table CREATE TABLE Users ( UserID int, ... ) Now I have several POCO classes with names ...

LINQ Syntext Sequence

The following SQL SELECT * FROM customers converted to this in LINQ var customers = from c in customers select c; Is their any good reasones why the from and select is swaped? The only logical reason I can think of is for intellisens? For the intellesens to get resolved, it needs to know what it is querying (scop...

LINQ group by question

I started playing around with Linq today and ran into a problem I couldn't find an answer to. I was querying a simple SQL Server database that had some employee records. One of the fields is the full name (cn). I thought it would be interesting to group by the first name by splitting the full name at the first space. I tried group by pe...

Getting a Linq-toSQL query to show up on a GridView

I have a pretty complicated Linq query that I can't seem to get into a LinqDataSsource for use in a GridView: IEnumerable<ticket> tikPart = ( from p in db.comments where p.submitter == me.id && p.ticket.closed == DateTime.Parse("1/1/2001") && p.ticket.originating_group != me.sub_unit select p.ticket ...

In-memory LINQ performance

More than about LINQ to [insert your favorite provider here], this question is about searching or filtering in-memory collections. I know LINQ (or searching/filtering extension methods) works in objects implementing IEnumerable or IEnumerable<T>. The question is: because of the nature of enumeration, is every query complexity at least ...

Efficiently merge string arrays in .NET, keeping distinct values

I'm using .NET 3.5. I have two string arrays, which may share one or more values: string[] list1 = new string[] { "apple", "orange", "banana" }; string[] list2 = new string[] { "banana", "pear", "grape" }; I'd like a way to merge them into one array with no duplicate values: { "apple", "orange", "banana", "pear", "grape" } I can d...

Good LINQ cheatsheets with VB.NET syntax?

I've been looking for a cheatsheet to help me remember LINQ syntax but the only ones I've found have been for C#. It seems like the C# syntax for LINQ is pretty different from the VB.NET syntax. Any suggestions for a good, concise list of LINQ syntax for VB.NET? ...

How do I use LINQ to query for items, but also include missing items?

I'm trying to chart the number of registrations per day in our registration system. I have an Attendee table in sql server that has a smalldatetime field A_DT, which is the date and time the person registered. I started with this: var dailyCountList = (from a in showDC.Attendee let justDate = new DateTime(a.A_DT.Year, a.A_DT.Mo...

Combining Lists in Lamba/LINQ

If I have variable of type IEnumerable<List<string>> is there a LINQ statement or lambda expression I can apply to it which will combine the lists returning an IEnumerable<string>? ...

How can I get an XElement's innertext in Linq to XML?

I'm trying to extract the polygons from placemarks in a KML file. So far so good: Imports <xmlns:g='http://earth.google.com/kml/2.0'&gt; Imports System.Xml.Linq Partial Class Test_ImportPolygons Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ...

LINQ query with multiple aggregates

How would I create the equivalent Linq To Objects query? SELECT MIN(CASE WHEN p.type = "In" THEN p.PunchTime ELSE NULL END ) AS EarliestIn, MAX(CASE WHEN p.type = "Out" THEN p.PunchTime ELSE NULL END ) AS LatestOUt FROM Punches p ...

LinqtoSQL filter and order by syntax

My Techie Bretheren (and Sisteren, of course!), I have a LinqToSql data model that has the following entities: I need to retrieve all advisors for a specific office, ordered by their sequence within the office. I've got the first part working with a join: public static List<Advisor>GetOfficeEmployees(int OfficeID) { List<Advisor>...

LINQ Submit Changes not submitting changes

I'm using LINQ to SQL and C#. I have two LINQ classes: User and Network. User has UserID (primary key) and NetworkID Network has NetworkID (primary key) and an AdminID (a UserID) The following code works fine: user.Network.AdminID = 0; db.SubmitChanges(); However, if I access the AdminID before making the change, the change neve...