linq-to-objects

Linq to objects

瘦欲@ 提交于 2019-12-08 00:44:39
问题 I have 2 databases from different servers. I cannot link the databases. The data is retrieved from the databases as : DB1 - Client_ID Engagement_ID Enabled Description 600 10 True Company1 600 20 False Company2 700 10 True Company3 DB2 Client_ID Engagement_ID Enabled Description 600 5 True Company1 600 10 False Company2 500 30 True Company3 The T SQL for this task is: select * from DB1 left join DB2 on DB1.client_ID = DB2.client_ID and DB1.Engagement_ID = DB2.Engagement_ID where DB2.CLient_ID

LINQ: Self join query, how to accomplish this?

瘦欲@ 提交于 2019-12-07 17:10:56
问题 Can anyone help? I have 1 class, basically it holds Members and within that class is a List. The members i have in a List also... So basically it goes like this, I have 2 members and each member has a number of sessions. I wish to only return each member with 1 Session. I have done a LINQ query, but of course it doesn't work... I think i need to do a self join, any ideas? Basically my error is m doesn't exist in my subquery self join. var sessions = from m in this.members join s in ( from se

C# Linq/Lambda expression: How to select an integer out of a string?

こ雲淡風輕ζ 提交于 2019-12-07 07:53:21
问题 I think the best way to explain my question is with a short (generic) linq-to-objects code sample: IEnumerable<string> ReadLines(string filename) { string line; using (var rdr = new StreamReader(filename)) while ( (line = rdr.ReadLine()) != null) yield return line; } IEnumerable<int> XValuesFromFile(string filename) { return ReadLines(filename) .Select(l => l.Substring(3,3)) .Where(l => int.TryParse(l)) .Select(i => int.Parse(i)); } Notice that this code parses the integer twice. I know I'm

How can I get LINQ to return the index of the object which has the max value in a collection?

人盡茶涼 提交于 2019-12-07 07:39:18
问题 I have a list of immutable objects (in my specific case a list of Tuple<double, double> ) and I'd like to change the one with the highest Item2 value. Ideally there would be an IndexOfMaxBy function I could use, so I could do: var indexOfPointWithHighestItem2 = myList.IndexOfMaxBy(x => x.Item2); var original = myList[indexOfPointWithHighestItem2]; myList[indexOfPointWithHighestItem2] = new Tuple<double, double>(original.Item1, original.Item2 - 1); I have seen How can I get LINQ to return the

Differences between LINQ to Objects and LINQ to SQL queries

我们两清 提交于 2019-12-07 06:05:22
问题 I have been using LINQ to query my POCO objects for some time, but I have not yet tried LINQ to SQL. I assume that LINQ to SQL queries are somehow converted to equivalent SQL queries and, given this, I am wondering if that affects the way LINQ to SQL queries are or should be written. Are there any significant differences between LINQ to Objects and LINQ to SQL that affect how I should write a query for either? 回答1: The main difference is as you say, LINQ to SQL queries are converted into SQL.

Linq statement to iterate through a collection and making sure each item is in the correct order?

北城以北 提交于 2019-12-07 04:56:28
问题 i'm got a simple IEnumerable<foo> foos; This collection are some results from a db call. I wish to make sure that each item in the list is ordered correctly. The Db either returns the results by ordered by id, lowest to highest. eg. 1, 2, 3, 4,. ... etc. ordered by date created, decrement (most recent, first). Can this be done with a quick linq statement .. to check for this? eg. Assert.IsTrue(.. insert linq statement here .. ) currently, i've got a for loop and remember the previous entry

LINQ-to-objects index within a group + for different groupings (aka ROW_NUMBER with PARTITION BY equivalent)

好久不见. 提交于 2019-12-07 02:32:38
问题 After much Google searching and code experimentation, I'm stumped on a complex C# LINQ-to-objects problem which in SQL would be easy to solve with a pair of ROW_NUMBER()...PARTITION BY functions and a subquery or two. Here's, in words, what I'm trying to do in code-- the underlying requirement is removing duplicate documents from a list: First, group a list by (Document.Title, Document.SourceId), assuming a (simplified) class definition like this: class Document { string Title; int SourceId;

group object with equal collections

倖福魔咒の 提交于 2019-12-07 00:32:29
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? One way is to build a comparable key out of the pets. For example, this method sorts the pets then combines them into a single string separated by '|' private static string

Calculate sum using LINQ

随声附和 提交于 2019-12-06 21:04:31
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 item in Items) { Cost = Cost + item.Costs.Sum(r => r.Cost); } I am not sure why you are messing it up

Count values in Dictionary using LINQ and LINQ extensions

安稳与你 提交于 2019-12-06 20:10:24
问题 I have a dictionary that looks something like this: Dictionary<String, List<String>> test1 : 1,3,4,5 test2 : 2,3,6,7 test3 : 2,8 How can I get a count of all the values using LINQ and LINQ extensions? 回答1: Let's say you have: Dictionary<String, List<String>> dict = ... If you want the number of lists, it's as simple as: int result = dict.Count; If you want the total count of all the strings in all the lists: int result = dict.Values.Sum(list => list.Count); If you want the count of all the