linq

Problem with LINQ to Entities query using Sum on child object property

Given this query: from s in services select new { s.Id, s.DateTime, Class = s.Class.Name, s.Location, s.Price, HeadCount = s.Reservations.Sum(r => r.PartySize), // problem here. r.PartySize is int s.MaxSeats } If the service doesn't have any reservations, this exception is thrown: System.InvalidOperatio...

Trying to Filter XML using XPath

I'm trying to filter an XML file using XPath. The XPath that I'm using is definitely filtering to the data that I want, but I'm just not sure how to filter the file overall. Here's the sample XML file: <fields> <field name='F'> <field name='0'><value>F.0 stuff</value></field> <field name='1'><value>F.1 stuff</val...

Query xml file without loading it into memory?

I am looking for something other than XmlReader. I want to apply a query and load only the data that is needed. Actually I want to load as little of the xml as possible. Maybe some kind of XQuery utility/class could do the trick. ...

Linq OrderBy Attribute Value and Group

I basically have a table with headers that I read in from a DB using linq in C#. Of these headers, there is always at least one that is always the same; Total and I want it to always be on the right. So here is sort of how my data is laid out: Data { Label, Value, Age //Not used initially } Sample Data: {"Dog", 7} {"Cat",...

Does Queryability and Lazy Loading in C# blur the lines of Data Access vs Business Logic?

I am experiencing a mid-career philosophical architectural crisis. I see the very clear lines between what is considered client code (UI, Web Services, MVC, MVP, etc) and the Service Layer. The lines from the Service layer back, though, are getting more blurred by the minute. And it all started with the ability to query code with Linq...

Another LINQ OrderBy query

I have a table (as in a list of numbers) that gets recreated through a series of linq queries. I'm trying to sort the columns in descending order based upon the values within the first row. My Database looks like this: Data { ColumnLabel, RowLabel, Value } {"Cat", "All", 9} {"Dog","All", 17} {"Fish", "All", 4} {"Cat", "Girl...

C# batching enumerator

Possible Duplicate: LINQ Partition List into Lists of 8 members. I've got an IEnumerable<T> and I'd like to transform it into an IEnumerable<List<T>> where each List is a batch of items in the same order as the original enumerator. Each batch should be batchSize items in length, except for the last batch which should contain l...

Unit Testing - How to Compare Two Paged Collections To Assert All Items Different?

Hi Guys, Had a quick look here, couldn't find a duplicate (correct me if im wrong). I've got the following Unit Test for some Paging with LINQ: // Arrange. const int locationId = 1; const LocationType locationType = LocationType.City; int pageSize = 10; // Act. var postsPageOne = PostService.FindAllPostsForLoc...

Linq Merge Queries

I have two queries that I would like to merge. This might be a left outer join, but it seems different. The first query selects distinct stuff from a table: var d = from d in db.Data select (d.ID, d.Label, Value = 0).Distinct; Lets suppose this returns the following: {1,"Apple",0} {2,"Banana",0} {3,"Cabbage",0} I then hav...

Select only the lowest values with Linq

Hi I got the following linq for ordering my input after lowest value. But i would like it to only output the lowest values. var sortedDict = (from entry in x where entry.Value > 0 orderby entry.Value ascending select entry); Now if it gets the following input. 3 4 2 6 2 This would be my output 2 2 3 4 6 What do I need to...

Traversing DataRelations in ASP.NET Gridview with ADO.NET and .NET 4

When one has a Gridview and two datatables and one wishes to get the parent row and bind it to a gridview, how would this be done? ...

Using nullable types in Linq expressions

var quantSubset = from userAns in userAnalysis.AllUserAnswers join ques in userAnalysis.AllSeenQuestions on userAns.QID equals ques.QID where (ques.QuestionType == "QT") select new { QuestionLevel = ques.LevelID, TimeTaken = userAns.TimeTaken, Points = userAns.Points, Us...

Collection to string using linq

Hello, I have a class public class Person { public string FirstName { get; set; } public string LastName { get; set; } } List<Person> PersonList = new List<Perso>(); PersonList.Add(new Person() { FirstName = "aa", LastName = "AA" } ); PersonList.Add(new Person() { FirstName = "bb", LastName = "BB" } ); I'd like get a stri...

Linq 2 xml: How to retrieve web methods' names from a wsdl document?

I have a XML documents (which describes a wsdl service's interface): <?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/m...

Split string to List<string> with Linq

Hello, I'd like avoid loop I have this : string s = "AAAA,12,BBBB,34,CCCCC,56"; With Linq, I'd like to have 2 List In the first : AAAA, BBBB and CCCCC In the second : 12,34 and 56 It's not based on numeric or not numeric. Thanks, ...

Generate unique groups for year/month/day/hour in a "yyyy.mm.dd.hh" string in linq

I have a list of strings on the format yyyy.mm.ddThh, I'd like to create a tree like structure of its components(Years, Months,Days,Hours) e.g. I have a list like: List<String> l = new List<String>() l.add("2010.10.11T10"); l.add("2010.10.11T11"); l.add("2010.09.01T23"); l.add("2009.01.02T03"); From this I'd like something like: new...

Linq Error on Left outer join

i have an alert table that has a 1:many mapping to devices. This relationship is conveyed ina mapping table. When I try to produce a left outer join from the mapping table to the various asset type tables i get the following error:System.Security.VerificationException: Operation could destabilize the runtime. var alertAssets = (from a ...

Update existing list values with values from another query

I have a linq statement which calls a stored proc and returns a list of items and descriptions. Like so; var q = from i in doh.usp_Report_PLC() where i.QTYGood == 0 orderby i.PartNumber select new Parts() { PartNumber = i.PartNumber, Description = i.Descritpion.Tri...

LINQ list to sentence format (insert commas & "and")

I have a linq query that does something simple like: var k = people.Select(x=>new{x.ID, x.Name}); I then want a function or linq lambda, or something that will output the names in sentence format using commas and "ands". {1, John} {2, Mark} {3, George} to "1:John, 2:Mark and 3:George" I'm fine with hardcoding the ID + ":" + Name...

Question about a terminology which implements something like this list.Add(new{a=1, b=2})

What is the terminology for the usage of "new" in: list.Add(new{a=1, b=2}) And what type should I replace the T in List getList if I want to use the list as the returned value? I don't want to replace T with "object" because I want to parse it in Linq query. Thanks. ...