linq

Executing a certain action for all elements in an Enumerable<T>

I'm getting started with C# 3.0 and LINQ, and I can't find a feature that to me is obvious, it's gotta be there. I have an Enumerable, and I want to execute something for each instance of it. Something like... string[] Names = ...; Names.each(s => Console.Writeline(s)); or Names.each(s => GenHTMLOutput(s)); // (where GenHTMLOutput c...

Linq + NHibernate: is it production ready?

Is Linq + NHibernate production ready? I hear many folks using it in production code but it is still officialy 'Alpha'. It has been a long time, however, so what's your experience? ...

LINQ dependent calculation assignment

I'm using the following pattern in C#: IList<foo> x = y.Select(a => new foo { b = Calc1(), c = Calc2() }).ToList(); foreach(foo f in x) { f.d = b / c; } What I would like to do though is: IList<foo> x = y.Select(a => new foo { b = Calc1(), c = Calc2() d = b / c; }).ToList(); So the question is: How can you ...

Using SelectedItem property of ComboBox w/Linq Anonymous Type.

In C# 3.5 using a ComboBox to display the results of a LinQ Query. How do I set the selecteditem property of the combobox when the LinQ query is returning an anonymous type? I set the datasource of the combobox along these lines: comboBox1.DataSource = from p in db.products select p; comboBox1.DisplayMember = "Name";...

Trying to change properties of an IQueryable collection

I am trying to do what I think is something simple, but I suspect I am simply too n00b to know that I am probably doing something wrong. I have a LINQ query return: IQueryable<CWords> Result Where CWords is a class I defined as follows: public class CWords { public CWords(){} public string _column1{ get; set; } public fl...

LINQ: Using INNER JOIN, Group and SUM

I am trying to perform the following SQL using LINQ and the closest I got was doing cross joins and sum calculations. I know there has to be a better way to write it so I am turning to the stack team for help. SELECT T1.Column1, T1.Column2, SUM(T3.Column1) AS Amount FROM T1 INNER JOIN T2 ON T1.T1ID = T2.T1ID INNER JOIN T...

LINQ-to-SQL select filter

Is there a way to ensure a particular conditional clause is added to the expression tree on each select from a particular table? For example, a table with a field with the date a record was deleted should never come back or be included in any kind of statement. Rather than include a where clause each time, is there a way, without cre...

Read attributes values with linq

I have an xml file that looks like the following. What I'm trying to do is to create a query that selects only the items with the attribute "Channel" and the value "Automotive". <item> <title>Industries</title> <category type="Channel">Automotive</category> <category type="Type">Cars</category> <category type="To...

linq grok'ed assumption check

right if my head is correct. LINQ is using similar syntax to SQL ie select from where join group +other functions. There reason this works is due to the <IEnumerable> and you create a list [table] of things - a collection of things in which you can interregate Is this correct or missed something? ...

Can I get the T-SQL query generated from a LinqDataSource?

I´m using the LinqDataSource to populate a grid. But now I need the SQL query that the LinqDataSource generates, to pass around throught methods (no, I can't modify the methods to not need a SQL query). Is there a way to obtain the generated SQL query from a instantiated and configured LinqDataSource? ...

LINQ injecting Invalid Characters

We are currently in the process of migrating from Server 2003 to Server 2008. We have a few different environment changes for our ASP.NET application. Our Test Environment is working perfectly at this time, but the production machine with same code is injecting Invalid Characters into the SQL. The following is the SQL I see via SQL ...

Are LINQ generated classes POCOs?

Are the classes generated by LINQ (DBML) considered POCOs? If I add static queries to these classes, are they still POCOs? I imagine it changes once I start doing business things in the LINQ partials. Such as adding other attributes, creating collections and basically making the partials more than DAL classes. If the LINQ classes ar...

LINQ to SQL: Return anonymous type?

Using the simple example below, what is the best way to return results from multiple tables using Linq to Sql? Say I have two tables: Dogs: Name, Age, BreedId Breeds: BreedId, BreedName I want to return all dogs with their BreedName. I should get all dogs using something like this with no problems: public IQueryable<Dog> GetDogs(...

Generalise LINQ query?

I have several queries identical except for the subject and parameters of the query and I am trying to formulate a 'generalised' query to avoid multiple lines of almost identical code. I have the following code: Code Snippet private IEnumerable doQuery(string rangeVar, DataSet dsSubTable, ...

Adding new methods to LINQ to SQL generated classes

I am new to LINQ. I just dragged all my database tables onto the designer in a LINQ to SQL dbml. All my relationships are correct and look nice in the designer. I am able to pull data using simple LINQ code. I want to add my own methods now but don't want to blow away my changes if (when) I need to regenerate my dbml. I am guessing ...

Linq-to-Sql: recursively get children

I have a Comment table which has a CommentID and a ParentCommentID. I am trying to get a list of all children of the Comment. This is what I have so far, I haven't tested it yet. private List<int> searchedCommentIDs = new List<int>(); // searchedCommentIDs is a list of already yielded comments stored // so that malformed data does not ...

Who has bought the autocompletion for linqpad?

Who has bought the autcompletion feature for Linqpad ? I know it's only $ 19 but I'd like to hear from you if it's worth it... Does it have any bugs? Does it really help in speeding up your linq queries development? Is there any limitations or any particular thing you might have found frustating? Any thoughts are appreciated .... ...

Ordering items by length in IEnumerable<T>?

I have a IEnumerable<T> collection with Name and FullName as items in it. There are around 5000 items in it. I want to display the FullNames sorted by its lenght, so first the longest name to the shortest name displays. How can I do it in most optimized manner? ...

How to name fields using Dynamic Linq?

I have a huge query that is being made dynamically, but I want the select statement to not output the column names, buut custom values. FOr example, if I am doing a normal Linq query, I can do something like this: var v = from p in db.items select new { name = p.item_name, price = p.item_price }; which will give me the nice '.name' a...

LINQ subquery with AND operator displays no results

Me again... I have an XML File that contains different categories which I would like to query for different attributes. <item> <title>Industries</title> <category type="Channel">Automotive</category> <category type="Type">Cars</category> <category type="Token">Article</category> <category t...