linq

TakeWhile using query syntax

Having public static IEnumerable<long> FibonacciNumbers() { long current = 0; long next = 1; while (true) { long previous = current; current = next ; next = previous + next; yield return current; } } I can get the first fibonacci numbers less that 100 with var series = FibonacciNumbers().TakeWhile(num => num < 100); J...

Linq query with Datetime Object

I want to receive the newest item from a collection. Each item has an DateTime field (EditDate) and I do a little query like that. var s = from l in OCollectionAgents where l.IDOfVehicle == agent.IDOfVehicle orderby agent.EditDate ascending select l; After the query I do agent.DetailInformationOfResults.NewestAgentEdi...

LINQ to dataset: CopyToDataTable()

Hi All, I want to query a datatable (dt) and load a 2nd dt with the resultant collection of datarows. Fine - we have the CopyToDataTable() extn mthd for exactly that purpose. However it is constrained to enumerate only over DataRows, which means that I cannot return anything else e.g. a collection of anonymous types. So - how can I modi...

combine items of two lists

I want to combine items of two string list but do not want repeated items List<string> l1 = new List<string>() { "A", "B", "C", "D"}; List<string> l2 = new List<string>() { "B", "E", "G", "D"}; Result: A, B, C, D, E, G How can i achieve this? ...

Data Binning with SQL or Linq

I am creating a custom reporting app in C# and need to aggregate some data. Let's make it simple. Say I have 10,000 sales for a year, I need a generic way to bin the data by say, month, day or hour. Each bin would therefore sum all the sales within that period. Has anyone ever written a query like this in either SQL or Linq? ...

Linq - Retrieve value from a linked linked table

Hi, I have the following model and relationship: Table: Granddad GranddadID GranddadName Table: Father FatherID GranddadID FatherName Table: Son SonID FatherID SonName in the Granddad controller: public ActionResult Edit(int tmpgranddadid) { var q = (from g in _e.Grandad where g.GrandadID == tmpgranddadid ...

Is there a faster way to check for an XML Element in LINQ to XML?

Currently I'm using the following extension method that I made to retrieve the values of elements using LINQ to XML. It uses Any() to see if there are any elements with the given name, and if there are, it just gets the value. Otherwise, it returns an empty string. The main use for this method is for when I'm parsing XML into C# objec...

Need help converting a TSQL query to EF using Linq

Here's the query: select FC.Title, COUNT(FSC.ID) as FCCount, COUNT(FP.NoOfSub), MAX(FP.LastPost) from ForumCategories FC left join ForumSubCategories FSC on FSC.CategoryID_FK = FC.ID left join (select SubCategoryID_FK, Count(ID) NoOfSub, MAX(DatePosted) LastPost from ForumPosts group by SubCategoryID_FK) FP on FP.SubCat...

Is there a faster way to check for an XML Element in LINQ to XML, and parse a bool?

FYI, This is very similar to my last question: Is there a faster way to check for an XML Element in LINQ to XML? Currently I'm using the following extension method that I made to retrieve the bool values of elements using LINQ to XML. It uses Any() to see if there are any elements with the given name, and if there are, it parses the val...

Failure to compile Linq Method in App_Code

Hi, I've been scratching my head for what seems like ages. I'm sure its really simple to fix but I can't see it. I have a class in App_Code that uses a bit of Linq. var siteMap = SiteMapWrapper.BuildSiteMap(true); var currentTopLevelParent = siteMap.Single(s => s.IsActive); if (currentTopLevelParent != null) I've developed this loc...

"IN" Operator in Linq

I am trying to convert an old raw Sql query in Linq with Entity Framework here. It was using the IN operator with a collection of items. The query was something like that: SELECT Members.Name FROM Members WHERE Members.ID IN ( SELECT DISTINCT ManufacturerID FROM Products WHERE Active = 1) ORDER BY Members.Name ASC Since the return o...

In C# problem grouping by value and then summing

I am trying to sum the cost for a series of items grouped by a person's organization. I believe the summation is working correctly but I am not seeing my grouping. The objects stored in rollup just contain the value of the summation. I'd expect them to have the organization in addition to the sum of the cost for that organization. What h...

How do I update a table with LINQ-to-SQL without having to delete all the existing records?

Lets say I have a simple table that only contains two columns: MailingListUser - PK ID (int) - FK UserID (int) I have a method called UpdateMailList(IEnumerable<int> userIDs). How do I, in LINQ, make inserts for the userIDs that are present in the passed in parameter but don't exist in the db, delete the ones that are in the...

Querying Nested Dictionaries using LINQ

I'm developing a POS system and I need to check whether the database tables on each terminal are out of sync. I maintain a Dictionary of terminal information, each of which has Dictionary containing table ids with a CRC for each table. Below is simplified description of what I've got so far (i'm using VB.NET but I've stripped out a lot...

Is there a faster way to check for an XML Element in LINQ to XML, and parse an int?

FYI, This is very similar to my last question: Is there a faster way to check for an XML Element in LINQ to XML, and parse a bool? Currently I'm using the following extension method that I made to retrieve the int values of elements using LINQ to XML. It uses Any() to see if there are any elements with the given name, and if there are, ...

Cannot serialize parameter of type 'System.Linq.Enumerable... ' when using WCF, LINQ, JSON

I have a WCF Service. It uses Linq-to-objects to select from a Dictionary. The object type is simple: public class User { public Guid Id; public String Name; } There is a collection of stored in a Dictionary<Guid,User>. I want to have a WCF OperationContract method like this: public IEnumerable<Guid> GetAllUsers() { ...

Property name repeated but with different data type

I have an table called Invoices with a column named Vendor. The Vendor column is a FK reference to a Vendors table with the PK being Id. My dbml creates the appropriate objects...Invoice and Vendor. However, my Invoice object has both a Vendor property (as a String) and a Vendor1 property (as a Vendor object). I thought it would have t...

Can a linq table be added to the asp .net cache?

And if so is there an example in vb .net? I want to add a linq result to the cache and then allow a mvc post to search the cache for better performance...every way I implement it I receive an object not referenced error... I'm confused, maybe I shouldn't be doing it this way but the mvc post will be checking up to 2000 records and it wo...

Passing nested data from Controller to View in ASP.Net MVC (LINQ)

I'm using LINQ to SQL (SQL Server) within ASP.Net MVC. My page needs to display all Regions and all Offices within each Region (nested). Offices belong to a Suburb, which in turn, belong to a Region. I.e. Within each Region, I need to grab the Offices which exist inside Suburbs which belong to the current Region in the loop. (Pseudo cod...

Is Linq "Where" more expensive than Linq "Select" ?

I'm investigating performance on an operation. I'm iterating a subset of items from a collection. I filter this collection using a Linq query. It basically looks like this: var filteredItems = items.Where(x => x.PropertyToFilterOn == filterValue); foreach (var filteredItem in filteredItems) { // do something to the filtered item } ...