linq-to-objects

How can I filter a dictionary using LINQ and return it to a dictionary from the same type

六月ゝ 毕业季﹏ 提交于 2019-11-29 22:35:06
I have the following dictionary: Dictionary<int,string> dic = new Dictionary<int,string>(); dic[1] = "A"; dic[2] = "B"; I want to filter the dictionary's items and reassign the result to the same variable: dic = dic.Where (p => p.Key == 1); How can I return the result as a dictionary from the same type [ <int,string> ] ? I tried ToDictionary , but it doesn't work. Thanks in advance. ToDictionary is the way to go. It does work - you were just using it incorrectly, presumably. Try this: dic = dic.Where(p => p.Key == 1) .ToDictionary(p => p.Key, p => p.Value); Having said that, I assume you

Call function in dynamic linq

╄→гoц情女王★ 提交于 2019-11-29 21:15:14
问题 I'm trying to call a function in a dynamic linq select statement, but im getting error: No property or field 'A' exists in type 'Tuple2' Example code: void Main() { var a = new Tuple<int, int>(1,1); var b = new[]{ a }; var q = b.AsQueryable().Select("A.Test(it.Item1)"); q.Dump(); } public static class A { public static int Test(int i) { return i++; } } How should I change my code to get this working? If I call built in function Convert.ToInt32 for example it works fine. var q = b.AsQueryable(

LINQ GroupBy continuous time

一个人想着一个人 提交于 2019-11-29 18:26:58
问题 Assuming I have a simple structure that looks like this: public class Range { public DateTime Start { get; set; } public DateTime End { get; set; } public Range(DateTime start, DateTime end) { this.Start = start; this.End = end; } } And I create an collection like so: var dr1 = new Range(new DateTime(2011, 11, 1, 12, 0, 0), new DateTime(2011, 11, 1, 13, 0, 0)); var dr2 = new Range(new DateTime(2011, 11, 1, 13, 0, 0), new DateTime(2011, 11, 1, 14, 0, 0)); var dr3 = new Range(new DateTime(2011,

Linq cross join query for nested List

怎甘沉沦 提交于 2019-11-29 17:46:23
Please do help me out in one of the scenario, where I got stucked. It goes like this. A dynamically created List of table and List of Fields( inside Table ) using PropertyGrid. BindingList<Table> table = new BindingList<Table>(); [Serializable] [TypeConverter(typeof(TableConverter))] public class Table { private string _name = string.Empty; private HeaderCollection _hc = new HeaderCollection(); private BindingList<Fields> _fc = new BindingList<Fields>(); public Guid key; public Table() { key = Guid.NewGuid(); } [DisplayName( "Table Fields" ), Editor( typeof( FieldCollectionEditor ), typeof(

LINQ OrderBy. Does it always return the same ordered list?

荒凉一梦 提交于 2019-11-29 13:55:17
I was trying out a simple OrderBy statement. The target data to order is something like below: [ {"id":40, "description":"aaa", "rate":1}, {"id":1, "description":"bbb", "rate":1}, {"id":4, "description":"ccc", "rate":2}, {"id":19, "description":"aaa", "rate":1} ] Then I order items by the rate property. The odd thing is that if I 'order' them, it 'skips' some items by a given offset and then 'take' only portion of the data. For example, var result = items.OrderBy(i => i.rate); var result = result.Skip(2); var result = result.Take(2); The result looks fine for the most of it, but the 'edge case

Does .GroupBy() guarantee order in its groupings?

百般思念 提交于 2019-11-29 13:26:24
Say I have an (ordered) sequence of animals: Eagle Elephant Tarantula Terrapin Tiger and I group by first letter: Animals.GroupBy(animal => animal.First()) will the elements of the IGrouping s in the resulting sequence be in the same order as the input sequence? Yes, they will be: GroupBy (MSDN) . The IGrouping<TKey, TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey, TElement>. Elements in a grouping are yielded in the order they appear in source. Quote from the MSDN page for GroupBy: The IGrouping<TKey,

Share expressions between Linq to Entities and Linq to Objects

非 Y 不嫁゛ 提交于 2019-11-29 12:39:51
I'm trying to "share" a set of conditions between a Linq to Entities call and a some other code, to reduce possible mismatches in conditions between the two calls. I started off by declaring my conditions: private Func<DateTime, Status, bool> _submissionDateExpiredCondition = (submissionDate, status) => submissionDate < DateTime.Now && status == Status.OK; private Func<DateTime, Status, bool> _submissionDateWithinOneWeekCondition = (submissionDate, status) => DateTime.Now < DbFunctions.AddDays(submissionDate, -7) && status == Status.Pending; private Func<DateTime?, Status, bool>

C# Merging 2 dictionaries

[亡魂溺海] 提交于 2019-11-29 11:10:10
问题 I'm developing an app in C# targeting .NET 3.5. In it, I have 2 similar dictionaries that contain validation criteria for a specific set of elements in my app. Both dictionaries have identical signatures. The first dictionary has the default settings and the 2nd dictionary contains some user defined settings. var default_settings = new Dictionary<string, MyElementSettings>(); var custom_settings = new Dictionary<string, MyElementSettings>(); I would like to combine the 2 dictionaries into one

LINQ - is SkipWhile broken?

走远了吗. 提交于 2019-11-29 10:51:45
问题 I'm a bit surprised to find the results of the following code, where I simply want to remove all 3s from a sequence of ints: var sequence = new [] { 1, 1, 2, 3 }; var result = sequence.SkipWhile(i => i == 3); // Oh noes! Returns { 1, 1, 2, 3 } Why isn't 3 skipped? My next thought was, OK, the Except operator will do the trick: var sequence = new [] { 1, 1, 2, 3 }; var result = sequence.Except(i => i == 3); // Oh noes! Returns { 1, 2 } In summary, Except removes the 3, but also removes non

Linq nested list expression

时光总嘲笑我的痴心妄想 提交于 2019-11-29 09:32:07
please I need your help with a Linq expression: I have nested objects with lists, this is how the main object hierarchy looks like (each dash is an atribute of the sub-class): Folder -name -List<Subfolder> Subfolders -name -List<Document> Documents -name -key Having this hierarchy of objects, I have a Document name, and I want to search for it and return its parent folder (Subfolder) Example: Folder -name: Customer -List<Subfolder> Subfolders -name: Personal -List<Document> Documents -name: Resume -key : 1 If I said: "Resume", the linq expression should return me: the subfolder "Personal" (the