linq

Linq GroupBy problem

I need to group data held in a list but can’t hardcode the groupBy clause as it mut be definable by the user. The example shown below works fine with the hardcoded “r.DeviceID” in the GroupBy statement. What I would like to do is to change this so that an end-user can select the field the expression will be applied to. At the moment the...

LINQ Advice - Using ToList().ForEach instead of 'classic' ForEach iteration

I would like to use LINQ's ForEach construct and am not sure how to convert the following. My current implementation: var employees = (from e in employeeDepartmentList select e.Employee).ToList(); employeeList = new EmployeeList(); foreach (var emp in employees) { employeeList.Add(emp); } I am thinking something like this: employ...

How to calculate multiple averages in one query in linq to entities

How to do this in linq to entities in one query? SELECT avg(Column1), avg(Column2), ... from MyTable where ColumnX = 234 ?? ...

Linq extension method, how to find child in collection recursive

Hi, I'm already familiar with Linq but have little understanding of extension methods I'm hoping someone can help me out. So I have this hierarchical collection pseudo code ie: class Product prop name prop type prop id prop List<Product> children And I have a list of products List products. Is there any way I can look for pr...

Linq to Entities 3.5 where most recent date

I have a table and for the sake of the example lets say it has three columns: Name, DateAdded, Note. In this table there will be multiple records with the same name. I need a linq where clause that will get me a result set with the most recent record for each name. So if I had the following information: Name, DateAdded, Note Alpha, 1/1...

LINQ query to ado.data entity model selecting more than one column in c#

ECommerceAdoEntities oContext = new ECommerceAdoEntities(); var lstnames = from c in oContext.tbl_ShippingProfile select c.Name; When I try to run a linq query against a ADO.net entity data model data source and return more than one column (c.Name,c.ID) it gives me error in C# But in VB I am able to do it. Dim adoDataE...

Telerik MVC grid - ArgugumentException when filtering

Greetings, I'm having the problem posted here (the post dated August 18th is mine), but not getting anywhere. In a nutshell, filtering with Telerik's MVC grid causes an ArgumentException with the message "The argument to DbIsNullExpression must refer to a primitive or reference type." Interestingly, if you .ToList() the IQueryable firs...

SQL Strategies for parent - child tables

I could use some help determining the best (most performant / easily maintainable) strategy for retrieving parent-child objects from a SQL db. I inherited this code, and I've got relatively short deadline on this and I want to do as little foundational changes as possible. I'll have plenty of time to implement nhibernate or other ORM wi...

linq xml error handling

i hope this is a simple question! in the following code all is well if the element exists but if not it errors out. XDocument xmldoc = new XDocument(); xmldoc = XDocument.Parse(response); XElement errorNode = xmldoc.Root.Element("TransactionErrorCode").Element("Code"); How can i test to see if it exists ...

Alternative to IEnumerable<T>.Skip(1).Take(1).Single()

I am having a difficult time with a seemingly easy and embarrassing problem. All I want is the next element in an IEnumberable without using Skip(1).Take(1).Single(). This example illustrates the basic problem. private char _nextChar; private IEnumerable<char> getAlphabet() { yield return 'A'; yield return 'B'; yield retur...

NHibernate query mapped collection

Dear All! In my ASP.NET web-application I use NHibernate to persist my "User"-Instances, where each of them has a "Entries" - collection. It is a typical one-to-many mapping and it works just fine. The mapping-code for the entries looks like this: <bag name="Entries" cascade="all-delete-orphan"> <key column="UserID" /> <one-to-ma...

LINQ Convert IEnumerable<string> to List<ulong>

I have the following code: var personIds = from player in myPlayers select player.person_id; where personIds is an IEnumerable<string> that I'd like to convert to List<ulong>, since person_id is convertable via Convert.ToUInt64() Is this easily done in LINQ? ...

Best way to check for duplicates when adding a new Row in DataSets?

Hey, What is the most effective way to add new items to my DataSet, and making sure that if it's an duplicate item, that I update it instead of creating a new entry? If I was writing this in mySQL I would use the 'On Duplicate' syntax. ...

How to use Linq/Lambda with ObservableCollection<T>

I have the following code that simply loops looking for a condition and places all matches into a new collection: ObservableCollection<Device> allDevices = GetAllDevices(); ObservableCollection<Device> matchingDevices = new ObservableCollection<Device>(); foreach (Device device in allDevices ) { if (device.ID != 5) matchingD...

How can I further simplify this piece of LINQ code

I have this code that I use to bind to a repeater: Repeater rpt; var q = from t in new[] { 10 } select new { ID = t }; rpt.DataSource = q; rpt.DataBind(); Is there a simpler way to accomplish this code segment; the var q part? ...

Entity Framework - An item with the same key has already been added. - Error when trying to define foreign key relationship

I have an entity with a foreign key relationship to an asp.net membership provider users table. This portion of the model looks like this: I can't seem to assign the foreign key relationship when inserting the Users table record, the record containing the foreign key to the aspnet_Users table. I keep getting the error: An item w...

Using Generics with LinqToObjects and NOT IN

I want to turn the follow function into a Lamda. After working on it for 45 minutes, I decided to go old school. How would one do this with a Lamda? public static void NotIn<T>(List<T> inListOne, List<T> notInListTwo,ref List<T> resultList) { resultList = new List<T>(); foreach (T item in inListOne) { if (notInListTw...

Entity Framework 4: Eager Loading (Include) with filters using Self Tracking Entities

I have a solution where I have created self tracking entities using the RTM templates. I have split the entities and context between 2 projects so that I can reuse the type definitions as I plan to run client/server via WCF. One of my service methods is required to return a graph of "Product" objects with child objects of "ProductSku" a...

Can I use LINQ to strip repeating spaces from a string?

A quick brain teaser: given a string This is a string with repeating spaces What would be the LINQ expressing to end up with This is a string with repeating spaces Thanks! For reference, here's one non-LINQ way: private static IEnumerable<char> RemoveRepeatingSpaces(IEnumerable<char> text) { bool isSpace = false; foreach ...

Meh, C# SortedList has no .Find

Silly if you ask me. But this message is here because I will assume (probably correctly) that I am the silly one, not Microsoft. So... is there something I'm missing? Why didn't they include a "Find" method to this baby? The find could work on the Values, which are objects, so then I could do this: someObject = SortedList.Values.Find(or...