linq

LinqToSql - Filter child and parent rows (alternate way?)

I need to filter a list of both parent and child rows and return the parent object with the filtered child objects. This is from a shared library so cannot be an anonymous type (var) I have a Parent object with Children (well, child objects). I want to return the parent object with parentId = 1 with a filtered set of child objects (it...

Databinding between XML file and GUI

I've got a problem with my little app here that is conceptually very simple. I have an XML file that essentially just maps strings to strings. Long-winded explanation warning A sample file would look something like this: <Candies> <Sees>Box1</Sees> <Hersheys>Box2</Hersheys> <Godiva>Box3</Godiva> </Candies> Although I coul...

LINQ sorting, doesn't work

I have a LINQ query like this: from i in _db.Items.OfType<Medium>() from m in i.Modules from p in m.Pages where i != null && i.Type == 1 && i.Published == true && p.PageId == 2 select p I use the query like this because I have strongly typed view (ASP.NET MVC). I need to have items sorted by the i.Sort property. orderby i.Sort and i....

Combining LINQ statements for efficiency

Hi, regarding linq to objects, if i use a .Where(x => x....) and then straight afterwards use a .SkipWhile(x => x...) does this incur a performance penalty because i am going over the collection twice? Should i find a way to put everything in the Where clause or in the SkipWhile clause? ...

linq populate custom collection

I have a collection defined as: public class MyCollection : List<MyItem> { ... } public class MyItem { ... } Using linq, I can use the Select method to return a IEnumerable, and I can call .ToList on that to get an IList but is there some way of getting back a type of MyCollection? Because I am tring to instantiate a class wh...

Django ORM's "related data" loading behavior

In LINQ (I come from a C# background), you can manually load data for related tables via the Include("xxx") method from a in ctx.MainTable.Include("SubTable") select a; In the above code, every instance of MainTable is loaded and all the data for MainTable.SubTable is also loaded. If "Include" is not called, every returned MainTable'...

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...

How to use CollectionAssert (and Linq?) to find all the values for one property, are the same?

Hi folks, this is an extension to a previous question I asked, today .... which highlighted the use of CollectionAssert to help test collections (which I never knew). I have an ICollection<Foo> foos; This has a property called Status, which .. to keep things simple, is an int or byte (whatever floats your boat <-- see what I did there?...

Using a session variable in the where clause of a LINQ statement

Good afternoon all. I have a page that displays data in a gridview based upon what the user selects for a material in a radiobutton list and how many they wish to see, a text box. Upon logging into this page, a session variable for MemberID is created, MemberKey. What I would like to do is pass this session variable into the LINQ stat...

Is using linq in this situation overkill

I am thinking about a re-factor, but I don't know if the end result is just overkill. Currently I have IList<myobjecttypebase> m_items; public int GetIncrementTotal() { int incrementTot; foreach(myobjecttypebase x in m_items) { incrementTot += x.GetIncrement(); } } Would it be overkill and/or less efficien...

Repository taking linq expression for filtering

I am considering refactoring a repository I have to improve its flexibility and reduce the method count. Where there are the following methods: Collection GetAllUsersByRole(Role role) User GetUserByuserName(string userName) ...I would like to have a single method taking a Linq expression: ICollection GetUsers(Expression e) { //ret...

C# Covariance issue

I had a linq-to-sql generated domain entity that I cast to the proper interface like so: public IEnumerable<IApplication> GetApplications() { using (var dc = new LqDev202DataContext()) { return dc.ZApplications.Cast<IApplication>().ToList(); } } However I renamed the linq-to-sql table withou...

linq performance: linq vs t-sql -- where does the time go?

I am testing the Linq performance now and I cannot figure out what for some time is wasted (MS-SQL Server 2005). So here is what I have: single table with clustered and non-clustered indices, all searches are done using columns which are covered in 100% by non-clustered index. I have 10 000 records, and all operations "touches" all reco...

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...

Quickest way to find the complement of two collections in C#

I have two collections of type ICollection; c1 and c2. I'd like to find the set of items that are in c2 that are not in c1 where the heuristic for equality is the Id property on MyType. What is the quickest way to perform this in C#. Edit: C# version = 3.0 ...

Getting linq error with group

I am getting the following error when I add the linq grouping. Error 5 'System.Linq.IGrouping' does not contain a definition for 'Description' and no extension method 'Description' accepting a first argument of type 'System.Linq.IGrouping' could be found (are you missing a using directive or an assembly reference?) using (var db...

dynamically varied number of conditions in the 'where' statement using LINQ

I'm working on my first project using LINQ (in mvc), so there is probably something very simple that I missed. However, a day of searching and experimenting has not turned up anything that works, hence the post. I'm trying to write a LINQ query (Linq to SQL) that will contain a multiple number of conditions in the where statement separa...

What XML related technologies I should know before learning LINQ to XML?

Hi I’d like to start learning Linq to Xml, but before I do that, are there any technologies I should know prior to learning Linq to Xml? Thus, besides the basics of XML and XML schema, which of the following technologies I must, which I would be advisable to and which I don’t need to learn before trying to learn Linq to XML: • Net Sch...

Is there something similar to python's enumerate for linq

In python I can easily get an index when iterating e.g. >>> letters = ['a', 'b', 'c'] >>> [(char, i) for i, char in enumerate(letters)] [('a', 0), ('b', 1), ('c', 2)] How can I do something similar with linq? ...

Can I use LINQ to retrieve only "on change" values?

What I'd like to be able to do is construct a LINQ query that retrieved me a few values from some DataRows when one of the fields changes. Here's a contrived example to illustrate: Observation Temp Time ------------- ---- ------ Cloudy 15.0 3:00PM Cloudy 16.5 4:00PM Sunny 19.0 3:30PM Sunny 19.5 3:1...