linq

How to Remove or Revert DataContext.DataLoadOptions?

First let me start by explaining my use case: Say there is a database "Cars". In that database, each row might have Make, Model, EngineType, etc. I have a page that is essentially a view of a single "Car" record, displaying its various stats. In that page, I have a user control that calculates and displys various MPG stats based on the ...

Split list at marker elements with LINQ?

I have a List<T> that is a mixture of 'regular' elements and 'marker' elements. I want to split it into a List<List<T>>, broken at the markers. e.g. given an input of {a, b, M, c, d, e, f, M, g}, where 'M' is a marker element, I want to get {{a, b},{c, d, e, f}, {g}} It is easy to do the job with a loop, but it seems like it should be ...

LINQ Join (Left Outer) with Take(1)

I have the below LINQ that is returning zero IF there aren't any Addresses(Inner Join). How would I make this an Outer Join and then only Take(1)? var results = query.Join( DB.tblAddresses.Where(t => t.AddressTypeID == 'm' || t.AddressTypeID == 'b').OrderByDescending(x => x.AddressTypeID), p => p.PersonI...

LINQ Partition List into Lists of 8 members.

How would one take a List (using LINQ) and break it into a List of Lists partitioning the original list on every 8th entry? I imagine something like this would involve Skip and/or Take, but I'm still pretty new to LINQ. Edit: Using C# / .Net 3.5 ...

Where is the "LINQ to SQL Overview" video featuring Luca Bolognese?

Hi all, I very much like the said video on LINQ, as it is very instructive to newbies on LINQ and I often shared that link. However, the video is no longer available (as it was) via the following link: http://www.microsoft.com/emea/msdn/spotlight/sessionh.aspx?videoid=716 ,which is referred to from http://blogs.msdn.com/b/lucabol/archi...

Convert collection of IEnumerable using LINQ

I have a IEnumerable collection of Car objects A Car has a property: Year Using LINQ, I want to find where there are > 1 cars with the same year and return that list. I would expect it to have to return an array of lists because if the collection is: Car 1: Year 2010 Car 2: Year 2010 Car 3: Year 2009 Car 4: Year 2009 Car 5: Year 2010...

How to get a Dictionary<string, List<Object> with LINQ?

I have an object that looks like this: class Model { public string Category {get;set;} public string Description {get;set;} } Currently I am getting the whole list of those objects with Linq, and then manually setting up a dictionary, like this: List<Model> models = //get list from repository and then with linq order them by...

EF Linq - Loading Relationship Values after Where Statement Part 2

this is a second part to my first question posted here... http://stackoverflow.com/questions/3757185/linq-query-after-where-statement-not-returning-relationship-data What Im trying to do is use linq to represent this data... Get My Friends' Favorite stores, where store is an object that contains a list of the friends who also have fav...

ASP.NET MVC2 with Entity Framework 4 - AsEnumerable() or ToList() in repository?

So I've been advised a few times to disable lazy loading when building an application with the above frameworks, and that ToList() will force the queries in my repository to execute. I was told that I would avoid certain "traps" I might run into if I used AsEnumerable(). On a recent question, however, I included a bunch of ToList()s ...

Refactor LINQ to Entities query

Hi, I've implemented a search-query. It replaces special chars with "normalized" ones. I've apply this rule on different fields. Actually, the query looks very ugly and is full of DRY-violations. But refacotring this, doesn't seem to be an easy thing (for me). Of course, I just tried to refactor the whole Replace-Stuff into a separate m...

.Net LINQ query syntax vs method chain

Hy Is there any performance difference between: (from item in collection where item.id == 3 select item) and collection.Where(item => item.id ==3) In general is there any performance difference between the linq syntax and the method chain? ...

Using LINQ, how do I choose items at particular indexes?

If I have an IEnumerable<Foo> allFoos and an IEnumerable<Int32> bestFooIndexes, how can I get a new IEnumerable<Foo> bestFoos containing the Foo entries from allFoos at the indexes specified by bestFooIndexes? ...

How do I call Enumerable.Join from F#?

Hi, I have two sequences (of tuples) on which I need to do a join: Seq 1: [(City1 * Pin1), (City2 * Pin2), (City1 * Pin3), (City1 * Pin4)] Seq 2: [(Pin1 * ProductA), (Pin2 * ProductB), (Pin1 * ProductC), (Pin2 * ProductA)] into the sequence (of tuples): [(City1 * ProductA), (City2 * ProductB), (City * ProductC), (City2 * Product A...

IEnumerable<string> to string

consider this: string test = ""; somestring.ToList().Take(50).Select( delegate(char x) { test += x; return x; } ); now why test is empty after that ? i dont care about the return of that actually i know its IEnumerable. an...

nested LINQ XML subquery not displaying values from root query in asp.net listview?

var subfacets = from l in facets.Descendants("Facet") let FacetName = l.Attribute("Name").Value let DisplayedFacetAttr = l.Attribute("DisplayedName") select new { DisplayedFacetName = (DisplayedFacetAttr != null) ? DisplayedFac...

IEnumerable is empty?

I know it probably doesnt matter/affect performance for the most part but I hate the idea of getting an IEnumerable and doing .Count(). Is there a IsEmpty or NotEmpty or some function? (similar to stl empty()) ...

linq way to insert element in order

hello, i have a collection of elements sorted by the elements' Name property. i need to insert a new element into the collection while maintaining the order. i am looking for a concise LINQ way to do this. my code is below. "this.Children" is the collection, and "d" is the new element that i need to insert. it takes two passes over the ...

linq extension method to take elements from the end of the sequence

Hi There is the enumerable extension method Take<TSource>( IEnumerable<TSource> source, int count ) which takes the first count elements from the start. Is there a way to take the elements from the end? or even better a way to take the elements from an offset to the end? Thanks ...

Check if LINQ entity of unknown type exists in its respective table.

Heres an interesting issue, I'm trying to check if a LINQ Entity exists in its table, but at design time I dont know what type that entity is. So I figure I'll just get the table, and try the Contains method on it. But I cant get the table in such a way that I can query it at design time. I've tried the GetTable method on the datacontex...

NHibernate.LINQ Supported Operators

I'm trying to evaluate NHibernate.LINQ 1.0 without actually writing any code. Ayende has admitted that this version of LINQ support is subpar compared to EF, but for the life of me I can't seem to find a page that explains what's supported and unsupported in this implementation. For example, can I use Skip & Take? What can I not use? ...