linq

converting ms sql "group by" query to linq to sql

i have two tables, products and categories. how do i convert the following sql query to linq format? select c.Name, c.DisplayName, count(p.id) from categories as c LEFT join products as p on p.categoryId = c.id group by c.Name, c.DisplayName some categories will have 0 products in them so the LEFT JOIN is important ...

How to deal with datatypes returned by LINQ

I'm new to LINQ, I've used LINQ to SQL to link to two tables, it does return data, which is cool. What I'm trying to understand is what datatype is being returned and how do I work with this datatype? I'm used to dealing with datatables. Are we throwing out datatables (and all the other ADO.Net object like rows, datasets etc.) now if ...

How to bind LINQ data to dropdownlist

The last two lines of this code do not work correctly -- the results are coming back from the LINQ query. I'm just not sure how to successfully bind the indicated columns in the results to the textfield and valuefield of the dropdownlist: protected void BindMarketCodes() { List<lkpMarketCode> mcodesList = new List<lkpMa...

ADO.NET Entity Framework generates unexpected, problem INSERTs

I need some help understanding the ADO.NET Entity Framework. I'm trying to represent and manipulate hierarchical data in a WPF TreeView control with the ADO.NET Entity Framework. Each of these Things has a single parent and zero or more children. My "delete" button... Private Sub ButtonDeleteThing_Click(...) db.DeleteObject(Di...

How do I do this SELECT statement that uses column renaming (or column alias) with LINQ?

Apologies if this has been asked already, but I couldnt find it. How do I get 'anotherColumnName' in LINQ? SELECT thisColumnName as anotherColumnName FROM TableName I have the following which obviously gives me 'thisColumnName' not 'anotherColumnName' var query = from names in _context.TableName select names; ...

Linq return IEnumerable<MyObject>, how to return MyListObject instead?

Hello, Is there a way without looping all the IEnumerable to get back the data inside the object (that inherited BindingList)? MyListObject--> Transformed with Linq --> Back the data inside MyListObject I know that I can do .ToList but it doesn't do what I would like. Any idea? Thank :) ...

better way to fill a dropdownlist with linq to xml

Hi, I have a bunch of dropdownlists which I populate from an xml file using linq. I've tried the following to populate them and it somehow works, the only problem is that when an element is missing e.g. subtitle I get an empty space in the dropdownlist. Also I don't get to have the distinct values of each dropdownlist but instead all ...

Strongly typed dynamic Linq sorting

I'm trying to build some code for dynamically sorting a Linq IQueryable<>. The obvious way is here, which sorts a list using a string for the field name http://dvanderboom.wordpress.com/2008/12/19/dynamically-composing-linq-orderby-clauses/ However I want one change - compile time checking of field names, and the ability to use refacto...

Numbering comments in ASP.NET and MSSQL

I've just thought about best way to store comments in database with appropriate numbers according to the article. The idea is to store comments with composite primary key (commentId, articleId) where commentId is generated according to the given articleId. The system of generating should has same principle as IDENTITY generated columns ...

Is DB_LINQ + Non-SQL Server Database an acceptable substitute for LINQ to SQL with SQL Server?

I'm trying to decide what database system to use for my next project which will be using VS.NET 2008 and will likely have a bit of LINQ code in it. No LINQ support may be a dealbreaker for any database system. Is DB_LINQ mature enough to allow other DBMS' to stand on even footing with SQL Server? Or should I just not even look into other...

Entity Linq - Retrieve record and only the first child record in a one to many relationship.

I have an entity called "Requests" which has a navigation called "StatusHistories" I need to retrieve all of the Requests where the last StatusHistory is "Open" StatusHistory has the fields StartDate (the highest one of these would be the last StatusHistory) Status (for this presume status contains the string "Open" or "Closed") Recor...

Concat all strings inside a List<string> using LINQ

Wondering if there is an easy LINQ Expression to concatenate my entire List collection items to a single string with a Delimiter character. UPDATE: What if the collection is of custom objects instead of String , Imagine I need to concat on object.Name ...

Should I use one LINQ DataContext or many?

For a large project, would it make sense to have one datacontext which maps out your database, which you can interact with from your classes? Or would it make more sense to split this up into small datacontexts which are focused on the specific tasks within the database that will be required. I'm curious as to the performance. It's my...

Linq with Left Join on SubQuery containing Count

I'm having difficulty translating sql to linq syntax. I have 2 tables (Category and CategoryListing) which reference each other with CategoryID. I need to get a list of all the CategoryID in Category Table and the Count of CategoryID for all corresponding matches in the CategoryListing table. If a CategoryID is not present in Category...

Navigating trees with LINQ to entities

I have a table called Area, that stores areas in a hierarchical manner. There is an Id and a ParentId column in the table. The ParentId is linked with a foreign key to the table's Id column. When I generate the EDM, I get two navigation properties, Area1 and Area2. How am I supposed to get back a tree structure of my areas, and navigate...

What about a SingleOrNew() method instead of SingleOrDefault() in LINQ?

The SingleOrDefault() method is great because it doesn't throw an exception if the collection you're calling it against is empty. However, sometimes what I want is to get a new object of some type if nothing exists. For example it would be great if I could do the following: var client = db.Clients .Where(c => c.Name == "Some Clien...

Linq compiled queries without passing the context

Consider this compiled linq-to-sql query: private static Func<LINQDBDataContext, string, IQueryable<Pet>> QueryFindByName = CompiledQuery.Compile(( MyLinqDataContext context, string name) => from p in context.Pets where p.Name == name select p); But I'm holding a private reference to context in the class already and I...

linq to entities inheritance query problem.

I'm trying to perform a linq to entities query on a table that's inherited using Table per Type. The problem I'm having is that I can't get at the properties on the Inhertied table only the properties on the Base Table. var qry = from i in _DB.BaseTable where i is catalogueModel.InheritedTable // Field Doesn't Exist // && i.InheritedTa...

How can I join the last X entries in a string array?

I've got an array like this: string[] parts = line.Split(','); string store = parts[0]; string sku = parts[1]; string subcatcode = parts[2]; string price = parts[3]; string date = parts[4]; string desc = parts[5]; I want description to equal the joined value of all the parts with an index of 5 or higher. Will this work or is there a...

Full Text Search in Linq

There's no full text search built into Linq and there don't seem to be many posts on the subject so I had a play around and came up with this method for my utlity class: public static IEnumerable<TSource> GenericFullTextSearch<TSource>(string text, MyDataContext context) { //Find LINQ Table attribute object[] info = typeof(TSour...