linq

Translate SQL SERVER SELECT to LINQ with multiple join Method Syntax

我的未来我决定 提交于 2020-06-28 03:58:51
问题 How could I do the join in linq from the following SQL SELECT statement by using method syntax : SELECT distinct [LAB_RESULTS].ORDER_ID ,LAB_RESULTS.patient_no ,Patients.PATIENT_NAME ,labtests.TestId ,labtests.TestName ,[RESULT_NUMBER] ,TestsRanges.LowerLimit ,TestsRanges.UpperLimit ,TestsUnits.UnitName FROM [dbo].[LAB_RESULTS] inner join LabTests on LabTests.testid=LAB_RESULTS.TESTID inner join TestsRanges on TestsRanges.TestId = LAB_RESULTS.TESTID inner join patients on Patients.Patient_No

Does LINQ AsParallel() preserve thread safety?

与世无争的帅哥 提交于 2020-06-28 03:15:28
问题 I'm having some doublts about concurrency using LINQ AsParallel() . Suppose I have the following code: int counter = 0; someList.AsParallel().ForEach(item => { doStuff(); counter++; }); I haven't found much online... Is it safe to do something like this? Is there a better way of doing this? Should I do some locking action for counter ? Thanks in advance 回答1: Is it safe to do something like this? ( counter++ ) No. There is no thread-safety to begin with, just code that is single-threaded. When

EF Core GroupBy with Select Distinct Count

放肆的年华 提交于 2020-06-27 16:05:29
问题 I have a table, Items , which has a many to one relationship with two distinct parents. I want to select the counts of ParentA for each ParentB . In SQL this is simple: SELECT "ParentBId", count(distinct "ParentAId") FROM "Items" GROUP BY "ParentBId" In Linq I have this statement: var itemCounts = await _context.Items .GroupBy(item => item.ParentBId, (parentBId, items) => new { ParentBId = parentBId, Count = items.Select(item => item.ParentAId).Distinct().Count(), }).ToDictionaryAsync(group =

C# LINQ append an item to the end of an array

 ̄綄美尐妖づ 提交于 2020-06-27 08:30:10
问题 I have an int[] array. I need to take an int and append it to the end of the array without affecting the position of the other items in that array. Using C# 4 and LINQ what is the most elegant way to achieve this? My Code: int[] items = activeList.Split(',').Select(n => Convert.ToInt32(n)).ToArray(); int itemToAdd = ddlDisabledTypes.SelectedValue.ToInt(0); // Need final list as a string string finalList = X Thanks for any help! 回答1: The easiest way is to change your expression around a bit.

What is the proper way to do a C# LINQ Where/Select in C++?

喜欢而已 提交于 2020-06-27 07:18:10
问题 In C#, if I have a List of objects (e.g. List myObjectList), I can get a subset of that list via: anotherMyObjectList = myObjectList.Where(x => x.isSomething()).Select(x => x).ToList(); Assuming I don't want to use a 3rd party C++ LINQ library (only standard library and maybe boost), what's the best way to do this in C++? It would be easy to write a function for each instance where I want to do this, but it would be better to know what framework exists to perform this type of operation. If

How to sort a lookup?

一曲冷凌霜 提交于 2020-06-27 07:14:17
问题 Hi I have a lookup type that stores strings and ints. static Lookup<string, int> lookup; lookup = (Lookup<string, int>)list.ToLookup(i => i.IP, i => i.Number); But now I need to sort this lookup by the values (number), and get the top 10 keys with their values. How is this possible? 回答1: I'm not sure why you are casting a Lookup<string, int> to a Lookup<string, string> , but the general answer you want is: var list = new List<Test> { new Test { IP = "A", Number = 1 }, new Test { IP = "A",

How to sort a lookup?

我只是一个虾纸丫 提交于 2020-06-27 07:14:10
问题 Hi I have a lookup type that stores strings and ints. static Lookup<string, int> lookup; lookup = (Lookup<string, int>)list.ToLookup(i => i.IP, i => i.Number); But now I need to sort this lookup by the values (number), and get the top 10 keys with their values. How is this possible? 回答1: I'm not sure why you are casting a Lookup<string, int> to a Lookup<string, string> , but the general answer you want is: var list = new List<Test> { new Test { IP = "A", Number = 1 }, new Test { IP = "A",

Using Linq in MongoDb C# how to perform nested Join [closed]

有些话、适合烂在心里 提交于 2020-06-27 06:39:11
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 days ago . Improve this question Giving these classes in C# Inbox.cs public class Inbox : TDocument { public string Name {get;set;} public List<MongoDBRef> WorkOrdersRef {get;set;} public List<InboxRule> Rules {get;set;} [BsonIgnore] public List<WorkOrder> WorkOrders {get;set;} } InboxRule.cs

Linq - group by using the elements inside an array property

筅森魡賤 提交于 2020-06-25 04:03:15
问题 I have a number of objects and each object has an array, I would like to group these objects by the values inside the array, so conceptually they look as follows: var objects = new []{ object1 = new object{ elements = []{1,2,3} }, object2 = new object{ elements = []{1,2} }, object3 = new object{ elements = []{1,2} }, object4 = new object{ elements = null } } after grouping: group1: object1 group2: object2,object3 group3: object4 somethings that I have tried: actual classes: public class

Linq - group by using the elements inside an array property

限于喜欢 提交于 2020-06-25 04:03:05
问题 I have a number of objects and each object has an array, I would like to group these objects by the values inside the array, so conceptually they look as follows: var objects = new []{ object1 = new object{ elements = []{1,2,3} }, object2 = new object{ elements = []{1,2} }, object3 = new object{ elements = []{1,2} }, object4 = new object{ elements = null } } after grouping: group1: object1 group2: object2,object3 group3: object4 somethings that I have tried: actual classes: public class