linq-to-objects

Using LINQ to parse the numbers from a string

对着背影说爱祢 提交于 2019-12-09 15:08:38
问题 Is it possible to write a query where we get all those characters that could be parsed into int from any given string? For example we have a string like: "$%^DDFG 6 7 23 1" Result must be "67231" And even slight harder: Can we get only first three numbers? 回答1: This will give you your string string result = new String("y0urstr1ngW1thNumb3rs". Where(x => Char.IsDigit(x)).ToArray()); And for the first 3 chars use .Take(3) before ToArray() 回答2: The following should work. var myString = "$%^DDFG

Fastest way to convert a list of strings into a single concatenated string?

微笑、不失礼 提交于 2019-12-09 10:03:43
问题 I have some LINQ code that generates a list of strings, like this: var data = from a in someOtherList orderby a select FunctionThatReturnsString(a); How do I convert that list of strings into one big concatenated string? Let's say that data has these entries: "Some " "resulting " "data here." I should end up with one string that looks like this: "Some resulting data here." How can I do this quickly? I thought about this: StringBuilder sb = new StringBuilder(); data.ToList().ForEach(s => sb

LINQ to Objects and improved perf with an Index?

烂漫一生 提交于 2019-12-09 05:27:53
问题 I am using LINQ to Objects and wonder if it is possible to improve the performance of my queries by making use of an index that I have. This is best explained with an example. Imagine a simple type... public class Person { public int Age; public string FirstName; public string LastName; } And a simple query I would make against it... List<Person> people = new List<Person>(); // 'people' populated with 50,000 instances... var x = from t in people where t.Age > 18 && t.Age < 21 select t; If I

How can I use Linq to to determine if this string EndsWith a value (from a collection)?

假如想象 提交于 2019-12-08 19:10:32
问题 i'm trying to find out if a string value EndsWith another string. This 'other string' are the values from a collection. I'm trying to do this as an extension method, for strings. eg. var collection = string[] { "ny", "er", "ty" }; "Johnny".EndsWith(collection); // returns true. "Fred".EndsWith(collection); // returns false. 回答1: var collection = new string[] { "ny", "er", "ty" }; var doesEnd = collection.Any("Johnny".EndsWith); var doesNotEnd = collection.Any("Fred".EndsWith); You can create

Why does EF throw “NotSupportedException: The method 'First' can only be used as a final query operation”

≡放荡痞女 提交于 2019-12-08 17:30:13
问题 int[] ids1 = { 1, 2, 3 }; int[] ids2 = { 1, 5, 6 }; var result = from a in ids1 where a == ids2.First() select a; foreach (var item in result) ; //ok var employees = from c in context.Employees. where c.EmployeeID == ids1.First() select c; foreach (var item in employees); // NotSupportedException When trying to call ids1.First within Linq-to-Entities query, I get an exception System.NotSupportedException: The method 'First' can only be used as a final query operation. Consider using the

How to partition a LINQ to Objects query?

谁说我不能喝 提交于 2019-12-08 17:25:06
问题 This is a resource-allocation problem. My goal is to run a query to fetch the top-priority shift for any time-slot. The dataset is very large. For this example, let’s say there are 100 shifts each for 1000 companies (though the real dataset is even larger). They are all loaded into memory, and I need to run a single LINQ to Objects query against them: var topShifts = (from s in shifts where (from s2 in shifts where s2.CompanyId == s.CompanyId && s.TimeSlot == s2.TimeSlot orderby s2.Priority

Is an object still connected to a list after FirstOrDefault?

孤街浪徒 提交于 2019-12-08 15:20:44
问题 Here's my code: Event thisEvent = (from i in list where (i.eventID == eventID) select i).FirstOrDefault(); if (thisEvent != null) { thisEvent.eventResolved = resolved; thisEvent.eventSequence.Add(item); } "list" is a collection of IEnumerable, i.e. IEnumerable<Event> list; What I'm wondering is: after creating thisEvent using FirstOrDefault, is thisEvent still connected to list? In other words, when I change the two properties, eventResolved and eventSequence, is "list" actually changed, or

Parallel.ForEach throws exception when processing extremely large sets of data

谁说胖子不能爱 提交于 2019-12-08 14:00:33
My question centers on some Parallel.ForEach code that used to work without fail, and now that our database has grown to 5 times as large, it breaks almost regularly. Parallel.ForEach<Stock_ListAllResult>( lbStockList.SelectedItems.Cast<Stock_ListAllResult>(), SelectedStock => { ComputeTipDown( SelectedStock.Symbol ); } ); The ComputeTipDown() method gets all daily stock tic data for the symbol, and iterates through each day, gets yesterday's data and does a few calculations and then inserts them into the database for each day. We use this rarely to recalculate static data values when a

group object with equal collections

◇◆丶佛笑我妖孽 提交于 2019-12-08 07:27:05
问题 Suppose 2 classes, Person and Pet. Each person has a collection of 1 or more pets. How do i group the Person in to a collection where they share the same pets. Example: Person 1: Cat, Dog, Spider Person 2: Cat, Spider, Snake Person 3: Dog Person 4: Spider, Cat, Dog Person 5: Dog What i want as a result is this: Group 1: Person 1, Person 4 Group 2: Person 3, Person 5 Group 3: Person 2 How do i achieve this using LINQ? 回答1: One way is to build a comparable key out of the pets. For example, this

Calculate sum using LINQ

自闭症网瘾萝莉.ら 提交于 2019-12-08 05:04:40
问题 foreach (Item item in Items) { Cost = Cost + item.Costs.OrderByDescending(x => x.ID) .FirstOrDefault() .Cost; } The above LINQ returns 150. I have 4 items(1,2,3,4) with the respective Cost. I need to rewrite the above LINQ to get the correct total cost of 210. ID--ItemID-- Cost 11 -- 1 -- 10 21 -- 2 -- 20 31 -- 2 -- 30 41 -- 3 -- 40 51 -- 3 -- 50 61 -- 4 -- 60 Can anyone please provide the modified LINQ to do so? Modifying the LINQ as below doesnt give me the expected result -- foreach (Item