linq

Sorting IQueryable by Aggregate in VB.net

Hello all, been searching for a quick example of sorting a IQueryable (Using Linq To SQL) using a Aggregate value. I basically need to calculate a few derived values (Percentage difference between two values etc) and sort the results by this. i.e. return rows.OrderBy(Function(s) CalcValue(s.Visitors, s.Clicks)) I want to call an exte...

DataSource containing a null value makes ComboBox fail

I've thrown myself headfirst into C# and .Net 2.0 using Linq, and I'm having a few problems debugging some of the problems, namely the following: I have a ComboBox control (cmbObjects) I want to populate with a set of objects retrieved using Linq. I've written a helper method to populate a List<T> generic: class ObjectProvider { p...

Cross join (pivot) with n-n table containing values

I have 3 tables : TABLE MyColumn ( ColumnId INT NOT NULL, Label VARCHAR(80) NOT NULL, PRIMARY KEY (ColumnId) ) TABLE MyPeriod ( PeriodId CHAR(6) NOT NULL, -- format yyyyMM Label VARCHAR(80) NOT NULL, PRIMARY KEY (PeriodId) ) TABLE MyValue ( ColumnId INT NOT NULL, PeriodId CHAR(6) NOT NULL, Amount DECIMAL(8, 4) NOT NU...

Linq with custom base collection

I often find linq being problematic when working with custom collection object. They are often defened as The base collection abstract class BaseCollection<T> : List<T> { ... } the collections is defined as class PruductCollection : BaseCollection<Product> { ... } Is there a better way to add results from a linq expession to this ...

Trying to do a sub query in Linq ... having issues!

I am trying to convert some of my stored procedures to Linq and am having problems with the following Transact-Sql statement: Select Year(p.StartDate) As Year, (Select Sum(t.Units) From Time t Where Year(t.TransactionDate) = Year(p.StartDate)) As Hours, (Select Sum(i.Price) From Invoice i Where Year(i.CreatedDate) = Year(p....

Newbie problem with LINQ in vb.net

Here is the single line from one of my functions to test if any objects in my array have a given property with a matching value Return ((From tag In DataCache.Tags Where (tag.FldTag = strtagname) Select tag).Count = 1) WHERE.... DataCache.Tags is an array of custom objects strtagname = "brazil" and brazil is definately a tag name s...

Best practice for returning IEnumerables from LINQ Methods

OK, This question has probably been answered before, but I'm not sure of how to word the Title. I have a class that has methods which return many composite LINQ queries. Most of these queries form Anonymous Types in order to get the data I need. I found out that I'm not able to return an Anonymous Type from a method, so I've been cre...

LINQ, iterators, selecting and projection

What I would like to do is use the elegance of LINQ while maintaining an iterator.... essentially Class A { int Position; string Name; } if I have a list of strings, i want to project them into List<A> but have the Position be populated in the projection... List<string> names; //filled with strings something like List<A> fo...

How do you count the words in an array of strings with LINQ?

Given an array like {"one two", "three four five"}, how'd you calculate the total number of words contained in it using LINQ? ...

How to do joins in LINQ on multiple fields in single join

I need to do a LINQ2DataSet query that does a join on more than one field (as var result = from x in entity join y in entity2 on x.field1 = y.field1 and x.field2 = y.field2 I have yet found a suitable solution (I can add the extra constraints to a where clause, but this is far from a suitable solution, or use this ...

Linq validation non nullable properties

I have Linq entities which contain non-nullable string properties. I'd like to iterate over the properties, perhaps in the partial OnValidate method, and set those properties to an empty string if they are null. How can I do this without explicitly writing code for each property by name? ...

Batch insert/update with entity framework

I have a Tags table whose schema consists of only ID and Name (unique). Now, from the GUI user can enter tags for a BlogPost. When the data is saved, with tags stored in an array of string (names), I want to add tags whose names don't yet exist to the Tag table and ignore tags whose names already exist AND get back the list of all tag...

'Contains()' workaround using Linq to Entities?

I'm trying to create a query which uses a list of ids in the where clause, using the Silverlight ADO.Net Data Services client api (and therefore Linq To Entities). Does anyone know of a workaround to Contains not being supported? I want to do something like this: List<long?> txnIds = new List<long?>(); // Fill list var q = from t in ...

refactoring the OrderBy expression

I would like to be able to refactor out the OrderBy clause in a linq expression. Here is an example of a refactor of the where clause before: results = ctx.ActiveUsers .Where(u => u.CompanyID != 1 && (u.LastName.ToLower().Contains(searchString) || u.Email.ToLower().Contains(searchString) || u.Co...

A Better DataTable

Hi All -- I have an application that uses DataTables to perform grouping, filtering and aggregation of data. I want to replace datatables with my own data structures so we don't have any unnecessary overhead that we get from using datatables. So my question is if Linq can be used to perform the grouping, filtering and aggregation of my...

Refactor select part of Linq expression?

I'm playing around with some Linq-SQL stuff, doing something like this: var foo = from f in db.Foo where f.Bar > 5 select f; which is all fine and dandy, and I know I can also do this: var foo = from f in db.Foo where f.Bar > 5 select new { f.Bar, f.Baz }; What I want to know, is can I factor out the select part of that query, if I...

Find frequency of values in an Array or XML (C#)

I have an XML feed (which I don't control) and I am trying to figure out how to detect the volume of certain attribute values within the document. I am also parsing the XML and separating attributes into Arrays (for other functionality) Here is a sample of my XML <items> <item att1="ABC123" att2="uID" /> <item att1="ABC345" att2="uID...

linqtosql will not allow updating of fields as it casts them as read only

I am having an issue with linq updating in linqtosql from the code below Dim lqPatientTable As New lqHospitalDataContext Dim strPatientId As String strPatientId = Me.ucboPatientInfo.SelectedRow.Cells(5).Value Dim lqPatientName = (From lqp In lqPatientTable.Patients _ Where lqp.PatientID = strPa...

LINQ: Cannot insert duplicate key row in object 'dbo.tblOutstandingCompletions' with unique index

Hello I have an application (ASP.NET 3.5) that allows users to rerun a particular process if required. The process inserts records into an MS SQL table. I have the insert in a Try / Catch and ignore the catch if a record already exists (the error in the Title would be valid). This worked perfectly using ADO but after I conveted to LINQ I...

Populate Excel with data from LINQ to SQL query.

I am trying to send some data from a LINQ query in C# to an Excel speed sheet using OLE I have a query like this: Var data = from d in db.{MyTable} where d.Name = "Test" select d; I have the Excel OLE object working fine, I just can't figure out how to populate the cells in Excel with the data from the LINQ quer...