linq-to-objects

Linq query help

I have two collections, and need to create a new collection from the two collections. Assume the following class: public class Widget { property int Id{get;set;} property string Label{get;set;} } We have two IList classes. I would like to create an Anonymous type with Id, Label, and Exists So doing this for Id and Label, I ha...

OrderBy and Top in LINQ with good performance

What is a good way to get the top 10 records from a very large collection and use a custom OrderBy? If I use the LINQ to Objects OrderBy method it is slow and takes a lot of memory because it creates an entire new collection with the new order. I would like a new method with the signature below that does not re-order the entire collect...

Group method not recognized in LINQ query

LINQ-newb having trouble using grouping. I'm trying to query an IEnumerable called dosesWithHighestScore. I'm following this example: http://msdn.microsoft.com/en-us/vcsharp/aa336747.aspx#minGrouped Here's my code: var doseWithLowestVolumeForForm = from d in dosesWithHighestScore group d by d.formulation.form into...

c# Linq Object subQuery / In

Hello, I have 2 objects that contains generic list properties. IE : public class User { public string Sid { get; set; } public List<Group> Groups { get; set; } } public class Section { public string Sid { get; set; } public List<Group> Groups { get; set; } } From my BLL I get a generic list of sections List mySection...

How to instantiate an object within a linq query

This is kinda theoretical question, I was looking at someone else' code (below) and my simple solution was to instantiate the collection outside linq, but I can guess there will be cases where I'd want to instantiate the objects inside the query, and perhaps only on a selection of elements. Here's a simplified example of how this was be...

Linq entity serialization problem

In my project we serialize disconnected Linq-to-sql entities(mainly to preserve them between postbacks). Code in use for that is fairly straightforward: public static string Serialize<P>(this P entity) { StringWriter writer = new StringWriter(); XmlTextWriter xmlWriter = new XmlTextWriter(writer...

Is there a way to use Linq projections with extension methods

I'm trying to use AutoMapper and a repository pattern along with a fluent interface, and running into difficulty with the Linq projection. For what it's worth, this code works fine when simply using in-memory objects. When using a database provider, however, it breaks when constructing the query graph. I've tried both SubSonic and Lin...

find inside array of objects

i have a objectA public class objectA { public int Id; public string Name; } i have a list of objectA List<objectA> list; i want to find in the list any objectA with Id = 10; is there linq syntax for this or do i simply have to write a loop here. ...

How to sort a List<T> by double value?

This sound simple but it not that much. I want to order a List based on one of the properties of T, which is double type. ...

LInq-to-sql dynamic sorting problem

I have a linq-to-sql query over entity that has child entityset that I need to sort on some child fields, i.e. use this query: var query = from p in context.Patients let order = p.Lab_Orders.First() orderby order.Order_Date select p; This query runs fine, but how would I modify it to use DLIN...

Group By either column using C# LINQ

I have a set of Data with columns such as below OffName,RO1,RO2,RO3 To explain further i use sample data as below: OffName RO1 RO2 RO3 A John Jack Rob B Earl John Carl C Rob Chris Kenny D Rodney Carl Jacob RO stands for Reporting Officer. Each Officer reports to upto 3 RO's.i need to make...

Determine whether two or more objects in a list are equal according to some property

Say I have a list List<MyObject> myObjectList. The MyObject object has a property named Order which is of type int. How can I determine whether two or more objects in myObjectList have the same Order using LINQ-to-objects? ...

C# linq/lambda expression: How to select an integer out of a string?

I think the best way to explain my question is with a short (generic) linq-to-objects code sample: IEnumerable<string> ReadLines(string filename) { string line; using (var rdr = new StreamReader(filename)) while ( (line = rdr.ReadLine()) != null) yield return line; } IEnumerable<int> XValuesFromFile(string f...

Linq statement to iterate through a collection and making sure each item is in the correct order?

Hi folks, i'm got a simple IEnumerable<foo> foos; This collection are some results from a db call. I wish to make sure that each item in the list is ordered correctly. The Db either returns the results by ordered by id, lowest to highest. eg. 1, 2, 3, 4,. ... etc. ordered by date created, decrement (most recent, first). Can this be...

LINQ to Objects - Grandparent, Parent, Child Relationship

am new to LINQ and I have been able to write a few simple statements. But now I have a more complicated situation which I cannot figure out. Basically, I am trying to write a LINQ to Objects statement where the relationship is a grandparent, parent, child relationship. (You could also call it a Master Detail relationship.) In Legacy co...

linq searching matching items in child collection

Hi I am looking for a way to do a IN clause query on child collections in linq. I have a an example as follows: Entity: Product.CategoryAssignments - this is an IList<Category> that the product is assigned to. Product can be assigned to multiple categories. I want to retrieve all products matching in a list of categories i.e. ILis...

[Optimize This]: Slow LINQ to Objects Query

I have this query that is bothering me; it is encapsulated as a new query operator, I made two versions of it, trying to see which one performs better. Both perform horribly. First attempt; declarative style public static IEnumerable<IEnumerable<α>> Section<α>(this IEnumerable<α> source, int length) { return source.Any() ? ...

How to do a "join" in a LINQ query against the Entity Framework

I have the following table structure which has been imported into the Entity Framework. I need to write a LINQ query where I select the entities of Table1, where a field in Table2 is equal to true, and a field in Table 3 is equal to a specific GUID. Could someone help with this? Thanks you. ...

query results cannot be enumerated more than once error

Hello I have a problem where I call a stored proedure twice, each with different parameters and I need 2 seperate lists but linq is caching the data and also giving me the error above I have tried 2 different methods to get round the error message, one using ToList() on tbl and the other manually walking through the data My code is sh...

How can I do Linq to SQL like relationships with Class Objects?

Right now I can do this with Linq to SQL response.write(car.models(0).makes(2).model.car.ID) of course you wouldn't actually do exactly this but I'm showing how I can go up and down the chain of relationships Now I want to do the same with simple Class Objects that aren't populated by data from some other source but I can't go "backwa...