linq

How do I convert a recursive object to a Collection in C#?

I have a recursive object, a linked list really: public class LinkedList { public string UniqueKey { get; set; } public LinkedList LinkedList { get; set; } } LinkedList will have some object graph that will eventually end in LinkedList.LinkedList == null. I would like to take all the objects in the graph and put them into a ...

Check if one IEnumerable contains all elements of another IEnumerable

What is the fastest way to determine if one IEnumerable contains all the elements of another IEnumerable when comparing a field/property of each element in both collections? public class Item { public string Value; public Item(string value) { Value = value; } } //example usage Item[] List1 = {new Item("1"),n...

Iterating DataView for Update

I have a DataView which filters inputView.RowFilter = "isnull(" + ID1 + ",'')<>'' and isnull(" + reason + ",0)=0"; after this filter i have to update "IsVerified" column of inputView to "1" Is there something in LINQ to execute the following? inputView.RowFilter.ForEach(x=>x.Field<IsVerified> =1); ...

LINQ - SELECT DISTINCT with NOT IN

I'm have a SQL statement which I am trying to transform in a LINQ statement... SELECT DISTINCT mc.* FROM ManufractorCategories mc WHERE mc.Active = 'true' AND mc.Folder = 'false' AND (mc.Id not in (SELECT Category_id FROM Manufractor_Category WHERE Manufractor_id = 3)); That's my last, not working LINQ ...

Need help in terminating deferred execution.

The following snippet prints 1 through 10 on the console, but does not terminate until variable 'i' reaches int.MaxValue. TIA for pointing out what I am missing. class Program { public static IEnumerable<int> GetList() { int i = 0; while (i < int.MaxValue) { i++; yield return i; ...

How to add children to an object graph in C#, given the parent?

I have several objects in C# that's roughly like this: A: { Id: 1, Parent: { Id: 2, Parent: { Id: 3, Parent: null }}} And, B: { Id: 4 Parent: { Id: 2 Parent: ...

Compare 2 lists using linq

I'm a linq noob.... can someone please some me how to achieve this using linq... I'm trying to compare 2 lists in both directions... internal void UpdateUserTeams(int iUserID) { UserTeamCollection CurrentTeams = GetUserTeams(iUserID); UserTeamCollection UpdatedTeams = this; foreach (UserTeam ut in Curre...

In Linq, how to find if a set contains an element without using Count(predicate)?

Hi, Since IEnumerable.Contains() method does not accept a predicate as an argument, Most people use the following code to check the existence of something matching a condition: // ProductId is unique. if (Products.Count(c => c.ProductId = 1234) == 1) { // Products list contains product 1234. } This code forces to walk through eve...

Select the elements, of the first node of XML (linq,C#)

Hello! My question is often asked at several places, but I have an xml, without attributes, so I can't use those methods. My XML's structure is this: <offers> <offer> <seller> <citizen> <name>A name</name> <id>An ID</id> </citizen> </seller> <amount>Number</amount> <exchange-rate>Rate</excha...

Using TryGetValue() in LINQ?

This code works, but is inefficient because it double-lookups the ignored dictionary. How can I use the dictionary TryGetValue() method in the LINQ statement to make it more efficient? IDictionary<int, DateTime> records = ... IDictionary<int, ISet<DateTime>> ignored = ... var result = from r in records where !ignored.Cont...

What part of the C# language is .ForEach()?

If I'm explaining the following ForEach feature to someone, is it accurate to say that #2 is the "LINQ foreach approach" or is it simply a "List<T> extension method" that is not officially associated with LINQ? var youngCustomers = from c in customers where c.Age < 30 select c; //1. traditiona...

Traversing a list, execute a method: Extension possible?

Hi, i have a data structure like this public class Employee { public string Name { get; set; } public IEnumerable<Employee> Employees { get; private set; } // ... } Now i need to loop through the complete structure and execute a method on each item. How can i create an extension on IEnumerable for such a traverse functio...

NHibernate Linq : Contains Statement

As an example, let's say you have a class like such **Person** int PersonID string PersonName BusinessLocation Locations **BusinessLocation** string city string state List<int> ZipCodes (saying that the locations may exist in multiple zipcodes) (also ignoring that zipcodes should be strings instead of ints, this is just an example) ...

How to read XML file using System.IO.Stream with LINQ

i will be passing the xml file like this: File1.PostedFile.InputStream //reading xml file..... public static void readXMLOutput(Stream stream) { System.Xml.Linq.XDocument xml = System.Xml.Linq.XDocument.Load(stream); var query = from p in xml.Element("ste").Element("Applicati...

Querying Excel Worksheets using LinQ - LinQToExcel

I'm using LinQToExcel to read my excel worksheets.ExcelQueryFactory has a method that returns a worksheet based on worksheet name(Only one value(Name) is allowed).There is also a method that returns WorkSheet names. Is there a way to use LinQ to select multiple worksheet collection based on names. ExcelQueryFactory test = new ExcelQuer...

LINQ to SQL many to many int ID array criteria query

Ok this should be really simple, but I am doing my head in here and have read all the articles on this and tried a variety of things, but no luck. I have 3 tables in a classic many-to-many setup. ITEMS ItemID Description ITEMFEATURES ItemID FeatureID FEATURES FeatureID Description Now I have a search interface where you can select a...

Is this a LINQ lazy loading problem?

Something very strange is happening in my program: I make this query agt.DefaultNr == 1 on a collection and get 3 items as Result: IEnumerable<Agent> favAgents = from agt in builtAgents where agt.DefaultNr == 1 select agt; For every item I set the DefaultNr = 0 foreach (Agent noFavAgt in favAgents) { noFavAgt.Default...

Group Number in a LINQ Group by Query

I've been using 101 LINQ Samples to get my feet wet using LINQ. It's been a good first resource, but I can't see an example there of what I currently need. I just need to associate a sequential group number with each group. I have a working solution: var groups = from c in list group c by c.Name into details select new { Name ...

Reading the XML values using LINQ

what is the best way of reading xml file using linq and the below code you will see that, I have three different loops and I feel like its not elegant or do I have options to retrofit the below code? public static void readXMLOutput(Stream stream) { XDocument xml = new XDocument(); xml = LoadFromStream(str...

selecting and skipping elements

I am trying to improve some code and I can't think of a better way to do what it currently does. essentially i loop outside for the number of items divided by the number I want to select, then inside that loop I select the item based on an inner loop of the number of items I need to create a single item modifying that by the outerloop va...