linq

Display data tables grouped and seperated by headings on an MVC view

I'm a complete C# AND MVC noob, so this one's been a bitter struggle for me. I've done a horrible but ultimately successful job of building a website for work to display the results of the impending primary/local elections. What I need in the final view of my site is to display all the results of races in the same category. For exampl...

Better Linq Query for filtering Parent List with no Children.

I have 2 Lists. var adultList = new List<Dude>(); adultList.Add(new Dude() { ID = 2, Name = "Randy Marsh" }); adultList.Add(new Dude() { ID = 3, Name = "Jimbo Kern" }); // no kids adultList.Add(new Dude() { ID = 4, Name = "Gerald Broflovski" }); adultList.Add(new Dude() { ID = 5, Name = "Stuart McCormick" }); adultList.Add(new Dude() { ...

Left Outer Join Result Issue Using Linq to Sql

We have the following query to give us a left outer join: (from t0 in context.accounts join t1 in context.addresses on new { New_AccountCode = t0.new_accountcode, New_SourceSystem = t0.new_sourcesystem, New_Mailing = t0.new_MailingAddressString } equals new { New_AccountCode = t1.new_AccountCode,...

LINQ: Selecting items from a list (Group By/Select/Sum & Max!)

Hi, Just getting my head around Linq and having lots of fun! Can any one aid me with a query for this: I have a list of data: Key Value Aaa 12 AaA 10 AAa 5 BBB 2 Bbb 1 1. I want to group by Key.ToUpper() 2. For every group I need the Max(Value) & Sum(Value) 3. For every group I want to select the entries T...

What wrong I am doing in IEnumerable(C#3.0)

If I write var v = (from r in stock.ReplacementLog select new { AssetId = stock.AssetId, Date = stock.ReferDate, ...

Problem with Linq Left Join

I have seen a number of examples on this but re-producting them in this example does not seem to work. Does anyone have any ideas what is wrong with the following code.... var products = new[] { new {ProductName ="Soda", Category = "Beverages"}, new {ProductName ="Tuna", Category = "SeaFood"}, new {Prod...

LINQ query expressions that operate on types (monads?) other than IEnumerable<T> -- Possible uses?

I'm reading the book Real-world functional programming by Tomas Petricek and Jon Skeet and I'm having a hard time digesting the section on computation expressions1) (aka monads). Through this book, I learnt that contrary to my previous experiences LINQ query expressions aren't restricted to IEnumerable<T>, but can work on other custom...

How to create Linq2Sql query, calculating aggregates?

Is there any way to create Linq2SQL query, that will be translated to this: SELECT COUNT(*) as totalCount , SUM(v.field1) AS totalfield1, .... SUM(v.<fieldN>) AS total<fieldN> FROM [dbo].[SomeTable] v ...

Using WDDCEDEEIWEB201to find associated timestamps from database

I have to following LINQ query, where I look for some different timestamps in an DB: var issues = from i in ReadOnlyContext.Issues where i.TruckID == truckID && i.OutOfOrderStart < startDate && i.OutOfOrderEnd > endDate || i.TruckID == truckID && i.OutOfOrderStart > startDate && i.OutOfOrderEnd < endDa...

How to Perform an Action on results of .FindAll()

Since I still have a limited knowledge of LINQ, I figured I would ask how to simplify this action. I am trying to write a statment that selects Customers from a list and performs some action on the results. Say I have: public List<Customer> Customers Customers.FindAll(delegate(Customer c) { return c.Category == "A"; }); Now say I...

why doesn't XElement have a ToDictionary linq expression?

For some reason ToDictionary isn't working. I am using vs.net 2010 and 4.0 XElement root = XElement.Load(fullPathToXml); IEnumerable<XElement> nodes = root.Elements("root"); var dic = nodes.ToDictionary(...) I want to convert it to a dictionary<string,string> it says there is no defintion for 'ToDictionary'...? ...

Except has similar effect to Distinct?

I just discovered that Except() will remove all elements in the second list from the first, but it also has the effect that it makes all elements in the returned result distinct. Simple way around I am using is Where(v => !secondList.Contains(v)) Can anyone explain to me why this is the behavior, and if possible point me to the documen...

Problem with Namespaces? Trouble When Using DynamicQuery Sample

DynamicQuery is a sample project that allows 'Dynamic' LINQ strings to be executed at run-time. I want to use this in a project of mine. I've created a new Window Forms Application in VB.Net; and add the existing item - 'Dynamic.vb' (taken from the DynamicQuery example). Once I do that, code that was previous fine, is now marked as in...

LINQ Numerical Grouping

Looking for a way to group in sets of n elements with LINQ. I.e: {1,2,3,4,5,6,7,8,9}: Grouped By 2: {{1,2},{3,4},{5,6},{7,8},{9}} Grouped By 3: {{1,2,3},{4,5,6},{7,8,9}} Grouped By 4: {{1,2,3,4},{5,6,7,8},{9}} Etc... Only way I can think of doing something like this at the moment is by using an anonymous type to generate a group in...

Linq and conditional sum

Hi, I have a class and a list as below: class C1 { int RecType ...; decimal Income ...; decimal Outcome ...; } List<C1> myList ...; The list is loaded with several records, and they have various values in RecType What I want is to calculate total on Income and Outcome but only for a certain value of RecType Here's a pse...

How to deal with the limited Linq support of OData (Open Data Protocol)

I have a software, which uses a lot of Linq-to-SQL. Recently, I want to migrate to OData / WCF Data Service architecture. But I met too many problems in the Linq support of OData - it is so limited. I have to modify most of my Linq statements and test them thoroughly again. I am wondering whether there is a system way to solve such a pr...

Exception with Linq2SQL Query

I am running a query using Linq2SQL that comes down to following query: DateTime? expiration = GetExpirationDate(); IQueryable<Persons> persons = GetPersons(); IQueryable<Items> subquery = from i in db.Items where i.ExpirationDate >= expiration select i; return persons.Where(...

linq subquery child collection to string

Hi All, i am trying to figure out how to write a linq query that will return a child collections "name" property as a string. I have a BO that has a "options" property where the options are the "name" property of each option in an "order" object. I would like the result to look something like order.id = 12312 order.date = 12/03/10 or...

To call SelectMany dynamically in the way of System.Linq.Dynamic

In System.Linq.Dynamic, there are a few methods to form Select, Where and other Linq statements dynamically. But there is no for SelectMany. The method for Select is as the following: public static IQueryable Select(this IQueryable source, string selector, params object[] values) { if (source == null) throw new Argument...

LINQ/C#: Where & Foreach using index in a list/array

Hi, I have a list/array and need to process certain elements, but also need the index of the element in the processing. Example: List Names = john, mary, john, bob, simon Names.Where(s => s != "mary").Foreach(MyObject.setInfo(s.index, "blah") But cannot use the "index" property with lists, inversely if the names were in an Array I c...