linq-to-objects

Linq to objects Predicate Builder

拈花ヽ惹草 提交于 2019-12-17 10:51:15
问题 What is the best way to do a conditional query using linq to objects(not linq to sql). Currently I am using the Predicate builder found here http://www.albahari.com/nutshell/predicatebuilder.aspx and passing the compiled predicate to the IEnumerable.Where and it seems to work nicely. Example code of what I want to solve: eg I have this string keyword1 = "Test1"; string keyword2 = "Test3"; IEnumerable<TestObject> tests = new List<TestObject>() { new TestObject() {Name1 = "Test1", Name2 =

Linq to Objects - return pairs of numbers from list of numbers

爷,独闯天下 提交于 2019-12-17 06:54:50
问题 var nums = new[]{ 1, 2, 3, 4, 5, 6, 7}; var pairs = /* some linq magic here*/ ; => pairs = { {1, 2}, {3, 4}, {5, 6}, {7, 0} } The elements of pairs should be either two-element lists, or instances of some anonymous class with two fields, something like new {First = 1, Second = 2} . 回答1: None of the default linq methods can do this lazily and with a single scan. Zipping the sequence with itself does 2 scans and grouping is not entirely lazy. Your best bet is to implement it directly: public

How to get the Point with minimal X from an array of Points without using OrderBy?

点点圈 提交于 2019-12-13 14:32:06
问题 Imagine I have var points = new Point[] { new Point(1, 2), new Point(2, 3) }; To get the point with the minimum X I could: var result = points.OrderBy(point => point.X).First(); But for large arrays, I don't think this is the faster option. There is a faster alternative? 回答1: It is better to use int x = points.Min(p => p.X); var result = points.First(p => p.X == x); as this eliminates the necessity of sorting this list (i.e., it is O(n) as opposed to, say, O(n log n) ). Further, it's clearer

Using LINQ with classes implementing non-generic ICollection

旧街凉风 提交于 2019-12-13 12:25:32
问题 I wanted to run a LINQ query against a MatchCollection object but found this wasn't possible as it doesn't implement ICollection<T> , just ICollection . What is the best option for using LINQ with non-generic collections, both in terms of code conciseness but also performance and memory usage? (If interested, here is the non-LINQuified code:) MatchCollection fieldValues = Regex.Matches(fieldValue, @"(?<id>\d+);#(?<text>[^;|^$]+)"); foreach (Match m in fieldValues) { if (m.Groups["text"].Value

how to bind data to listbox in wp7

余生颓废 提交于 2019-12-13 04:45:13
问题 i am binding data to listbox in wp7 here is the code <ListBox x:Name="list_budget" Width="440"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Name="txtname" Text="{Binding category}"></TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox> //class function public string[] jinal; public void budgetcategorywise() { var q = from shoppingItem p in db.Item1 group p by new { p.category_name } into g select new { category = g.Key, total = g.Sum(p => p.total_amt) `enter code here`}.ToString

linq query close my wpf application

为君一笑 提交于 2019-12-13 04:28:10
问题 I have a wpf application. when I run it it close. I debugged it and I found that this linq query close it(I don't know why!) TodayCards = cards.Where(i => (i.NextTime.Day == DateTime.Now.Day && i.NextTime.Month == DateTime.Now.Month && i.NextTime.Year == DateTime.Now.Year)).Select(i => i).ToList(); I also tried TodayCards = cards.Where(i => (i.NextTime.Day == DateTime.Now.Day && i.NextTime.Month == DateTime.Now.Month && i.NextTime.Year == DateTime.Now.Year)).ToList(); but it closed it both

convert ienumerable linq list to typed list

拟墨画扇 提交于 2019-12-13 00:35:37
问题 I'm having the following issue: I'm using linq to filtrate some data in this way: var listPerson = (from objPerson in ListPerson select new { objPerson.IdPerson, objPerson.ApePerson, objPerson.NomPerson, objPerson.EdadPerson, objPerson.Gender }).Distinct(); Everything works fine, but the problem is when i tried to cast the listPerson List to a List<PersonClass> How can i do that cast ? I tried: listPerson.ToList(); But it throws an exception (cannot convert from IEnumerable to IEnumerable).

Distinct by part of the string in linq

梦想与她 提交于 2019-12-12 12:39:03
问题 Given this collection: var list = new [] { "1.one", "2. two", "no number", "2.duplicate", "300. three hundred", "4-ignore this"}; How can I get subset of items that start with a number followed by a dot ( regex @"^\d+(?=\.)" ) with distinct numbers? That is: {"1.one", "2. two", "300. three hundred"} UPDATE: My attempt on this was to use an IEqualityComparer to pass to the Distinct method. I borrowed this GenericCompare class and tried the following code to no avail: var pattern = @"^\d+(?=\.)

Unable to Cast() generic dictionary item to DictionaryEntry when enumerated via non-generic IDictionary

≯℡__Kan透↙ 提交于 2019-12-12 12:24:55
问题 I have some code that iterates over a non-generic IDictionary by first calling the LINQ Cast method. However I get an invalid cast exception when passing a generic dictionary implementation even though I'm specifically using it via the non-generic IDictionary interface. IDictionary dict = new Dictionary<object, object> {{"test", "test"}}; foreach (var item in dict) { Debug.Assert(item is DictionaryEntry); // works } dict.Cast<DictionaryEntry>().ToList(); // FAILS Why does the Cast method

Possible to write a Join between Sql and DataTable using Linq?

£可爱£侵袭症+ 提交于 2019-12-12 10:22:19
问题 I have a process that extracts customer info from multiple databases (MySql) based on a timestamp. I store this data into a DataTable . The data table represents updates to existing customer info as well as new customer info. I want to delete any dupes in the destination database (SqlServer) based on one constant value, CompanyID , and the CustomerID . So, I thought a join would give me the RecordIDs of the dupes in the destination DB, pass the List<int> (or some collection mechanism) to the