todictionary

Lambda way to fill the value field in the ToDictionary() method?

I have two IEnumerable IEnumerable<MyObject> allowedObjects = MyService.GetAllowedObjects(); IEnumerable<MyOBject> preferedObjects = MyService.GetPreferedObjects(); We can safely assume that preferedObjects will always be a subset of allowedObjects. I want to create an IDictionary<MyObject, bool>. Objects where the key is the set of M...

Convert Linq Query Result to Dictionary

Hi, I want to add some rows to a database using Linq to SQL, but I want to make a "custom check" before adding the rows to know if I must add, replace or ignore the incomming rows. I'd like to keep the trafic between the client and the DB server as low as possible and minimize the number of queries. To do this, I want to fetch as little...

Does Enumerable.ToDictionary only retrieve what it needs?

I'm using Enumerable.ToDictionary to create a Dictionary off of a linq call: return (from term in dataContext.Terms where term.Name.StartsWith(text) select term).ToDictionary(t => t.TermID, t => t.Name); Will that call fetch the entirety of each term, or will it only retrieve the TermID and the Name fields from my data...

How do I use Linq ToDictionary to return a dictionary with multiple values in the dictionary items?

I want to group items from a linq query under a header, so that for each header I have a list of objects that match the header title. I assumed the solution would be to use ToDictionary to convert the objects, but this allows only one object per "group" (or dictionary key). I assumed I could create the dictionary of type (String, List Of...

C# Custom Dictionary Take - Convert Back From IEnumerable

Scenario Having already read a post on this on the same site, which didn't work, I'm feeling a bit stumped but I'm sure I've done this before. I have a Dictionary. I want to take the first 200 values from the Dictionary. CODE Dictionary<int,SomeObject> oldDict = new Dictionary<int,SomeObject>(); //oldDict gets populated somewhere...

C# convert an IOrderedEnumerable<KeyValuePair<string, int>> into a Dictionary<string, int>

I was following the answer to another question, and I got: // itemCounter is a Dictionary<string, int>, and I only want to keep // key/value pairs with the top maxAllowed values if (itemCounter.Count > maxAllowed) { IEnumerable<KeyValuePair<string, int>> sortedDict = from entry in itemCounter orderby entry.Value descending s...

Filter Custom Dictionary with LINQ ToDictionary - "Unable to cast object of type 'System.Collections.Generic.Dictionary`2"

I have created a Dictionary class (MyDictionary for the example). I am currently trying to pass MyDictionary into a function, filter it into a new instance of MyDictionary and pass this new instance into another method. When I am attempting to create the second instance from the filtered first instance of MyDictionary via Lambda Expres...

Is there a better way to aggregate a dictionary using LINQ?

I am trying to build a dictionary from an enumerable, but I need an aggregator for all potentially duplicate keys. Using ToDictionary() directly was occasionally causing duplicate keys. In this case, I have a bunch of time entries ({ DateTime Date, double Hours }), and if multiple time entries occur on the same day, I want the total ti...

LINQ to SQL ToDictionary method

I have a table with a structure like this: Column1 AS INT, Column2 AS INT, Column3 AS INT, Column4 AS DECIMAL My LINQ query is this: var query = from t in context.Table select t; I want to convert/transform the query to this Dictionary object: Dictionary<int, Dictionary<int, Dictionary<int, decimal>>> using only the ToDictionary...