linq

LINQ Equivalent for Standard Deviation

Does LINQ model the aggregate SQL function STDDEV() (standard deviation)? If not, what is the simplest / best-practices way to calculate it? Example: SELECT test_id, AVERAGE(result) avg, STDDEV(result) std FROM tests GROUP BY test_id ...

Aggregate functions with a left outer join in LINQ to Entities

I've been looking through related LINQ questions here trying to figure this one out, but I'm having some trouble converting a SQL query of mine to the equivalent LINQ to Entities version. select companies.CommpanyName, job.Position, count(offers.jobID) As Offered, job.Openings, job.Filled from jobs left outer...

How to get properties from anonymous type in VB.NET

I am trying to figure out how to get at an anonymous typed object's properties, when that anonymous type isn't created in the current function. Specifically, I am binding an ASP.NET ListView to LINQ resultset, then trying to process each item in the ItemDataBound event. Option Explicit On Option Strict On Class MyPageClass Privat...

How can I databind to an asp.net treeview using linq to entities?

I have seen other similar questions dealing with WPF Almost exactly the same but I can;t figure this out in ASP.net. I have a Pages table with a parentpage foreign key and want to databind them to a treeview. In the past I have created a hierarchicaldataset from a dataset but would like to stay within the entities framework if I can. ...

Hacker News style ordering algorithm in Linq-To-SQL

According to this site the ordering algorithm for hacker news goes something like this: (p - 1) / (t + 2)^1.5 Description: Votes divided by age factor p = votes (points) from users. t = time since submission in hours. p is subtracted by 1 to negate submitters vote. age factor is (time since submission in hou...

Combobox for Foreign Key in DataGridView

I have a database containing two tables, Products and Licences. Licences.ProductID has a foreign key reference to Products.ProductID (i.e. licenses for that product). How do I represent that relationship in a WinForms DataGridView? When feeding the DtaaGridView (SQL Metal and through LINQ to SQL), the ProductLicences.ProductID, it auto...

Why is this linq extension method hit the database twice?

Hi folks, I have an extension method called ToListIfNotNullOrEmpty(), which is hitting the DB twice, instead of once. The first time it returns one result, the second time it returns all the correct results. I'm pretty sure the first time it hits the database, is when the .Any() method is getting called. here's the code. public stati...

LINQ intellisense stopped working

What happend to my Intellisense?? When I type a line like this ... Dim users = (From u In Membership.GetAllUsers Select u.UserName) ... I get (almost) no Intellisense when I get to the Select u. part. Only Equals, GetHashCode, GetType, ReferenceEquals and ToString appears. Not "UserName" and the other relevant propeties of the Membe...

Get an IDataReader from a typed List

I have a List<MyObject> with a million elements. (It is actually a SubSonic Collection but it is not loaded from the database). I'm currently using SqlBulkCopy as follows: private string FastInsertCollection(string tableName, DataTable tableData) { string sqlConn = ConfigurationManager.ConnectionStrings[SubSonicConfig.DefaultDataP...

Is LINQ-to-NHibernate ready for production code ?

Simple question: Is LINQ-to-NHibernate stable and feature-complete enough to be used for production code ? What are the limitations ? NOTE : I am aware that this is a duplicate of this question, but the last answer is over a year old, so the answers might or might not be relevant any longer. ...

Remove duplicates by field from one table using another using LINQ

I have to leave in a DataTable only records with dates currently not present in the database. So I read all existing dates using the stored procedure (is it correct?): SELECT DISTINCT CAST(S.[date] AS DATE) -- original date is DATETIME2(0) FROM ... WHERE ... and load it to a DataTable: var tableDate = new DataTable(); new SqlDataAda...

Select Distinct List of Words from Array with LINQ

Hi All I'm trying to get a distinct list of words from an array of words with the following code: string words = "this is a this b"; var split = words.Split(' '); IEnumerable<Word> distinctWords = (from w in split select new Word { ...

linq to entity - include with lambda expression

I have a lite problem i don't really know how to fix. In my example below i would like to select a list of ProductCategories with ProductItems that are active. public IEnumerable<ProductCategory> ListProductCategories() { return _entities.ProductCategorySet.Include("ProductItems").Where(x => x.ProductItems.Active == ...

Can this linq statement be refactored to return an IQueryable instead?

Hi folks, i have the following linq extension method, which returns an IEnumerable. This means, that this piece of code ends up hitting the DB, before the rest of the linq statement has been finished. Is it possible to refactor this so it returns an IQueryable instead? public static IEnumerable<TSource> WhereIf<TSource> (this IEnu...

How to find the first item according to a specific ordering using LINQ in O(n)?

Suppose I have a list of items (e.g., Posts) and I want to find the first item according to some non-trivial ordering (e.g., PublishDate and then CommentsCount as a tie-breaker). The natural way to do this with LINQ is like this: posts.OrderBy(post => post.PublishDate).ThenBy(post => post.CommentsCount).First() However, the micro-opti...

Insert linq-to-sql problem?

I'm playing around with the Northwind database and by reading some tutorials I see that Categories should have an Add method, but it ain't working. Something is missing and I would appreciate someone else who knows more for some feedback. public void AddCategory(string categoryname) { string connstring = WebConfigurationMana...

Can someone show me example for running a stored procedure twice and get diff results?

Hello For the past few weeks I have been really struggling doing something that should be really simple! I have a stored procedure in a database which I call twice. Each call results in totally different data. I need the entity framework to treat it like this At the moment it caches everything So I am going to ask for help in a diff...

Is LINQ mostly for Select statements?

I think LINQ feels best with select statements and making a new object/record. For updating and deleting I don't feel it is really easy or convenient. Anybody who has any views on LINQ's use? ...

Is there a shorter way to parse attributes as Int (or any other primitive) in LinqToSql

I was basically wondering if it is possible to optimize this code: AmountComments = Int32.Parse(r.Attribute("AmountComments").Value) Ideally I would want to write something like AmountComments = r.Attribute("AmountComments") r would be a xml tag of type XElement, which is selected before in a Linq query. ...

How widely used Linq is in the industry ?

It's been some time since Linq to objects has been introduced and i'm curious about it's adoption among developers. From my experience I can't say i have seen a lot of code using it for it's filtering, ordering, and grouping capabilities. Is it the same for you ? Does developers really find the style of programming it enables more con...