C# order a list by another list
WebDec 19, 2024 · \$\begingroup\$ Further to the comment about automagic projection: I would be inclined to separate that functionality out into it's own 'module' (if not using a pre-built one), so that you can retrieve a translator between T1 and T2 (e.g. Func Mapping.Mapper()).My experience with reflection has always been that … WebNov 11, 2012 · names.Select ( (n, index) => new { Name = n, Index = index }) .OrderBy (x => numbers.ElementAtOrDefault (x.Index)) .Select (x => x.Name) .ToList (); But i would use another collection type like Dictionary instead if both lists are related …
C# order a list by another list
Did you know?
WebApr 2, 2013 · c# - Find items from a list which exist in another list - Stack Overflow Find items from a list which exist in another list Ask Question Asked 10 years ago Modified 2 years, 7 months ago Viewed 93k times 53 I have a List PropA { int a; int b; } and another List PropX { int a; int b; } WebMay 1, 2024 · Another approach is to use overload of the method Array.Sort with IComparer. At first we should implement IComparer: private class PeopleComparer : IComparer { public int Compare (Person x, Person y) { return x.Name.CompareTo (y.Name); } } And then we can sort two our arrays:
WebDec 26, 2012 · Since a List is a reference type, what is passed to the function is a reference to the original list. See this MSDN article for more information about how parameters are passed in C#. In order to achieve what you want, you should create a copy of the list in SomeOperationFunction and return this instead. A simple example: WebMay 28, 2013 · C# Check if a List is a part of another List [duplicate] Ask Question Asked 9 years, 10 months ago. Modified 4 years, 6 months ago. Viewed 25k ... Given two IEnumerables it will return a list of any values that exist in both. var presentInBoth = query1.Intersect(query2) You may well need to call .ToList() query1 and query2 to make …
WebOct 17, 2013 · The first list will define the order for the matched objects - the actual order doesn't matter, so long as both lists are ordered the same. Example below [pseudocode] shows what I mean, where the second list is sorted by TargetId to … WebMar 7, 2016 · 1 Answer. You can do select the index of the item, throw it into an anonymous object order by that and then select the string again : var res = kws .Select (a=>new {orderOf=order.IndexOf (a.fkp_keyword),val=a}) .OrderBy (a=>a.orderOf) .Select (a=>a.val) .ToList (); If you have items that are not in the list you are ordering by then they will be ...
WebMar 18, 2024 · IList Items = new List (); Items.Add ( new Item () {Id = 10, Name = 'X'}); Items.Add ( new Item () {Id = 11, Name = 'Y'}); Items.Add ( new Item () {Id = 12, Name = 'Z'}); Items.Add ( new Item () {Id = 13, Name = 'A'}); //Comparing list // The below list contains Ids but not in a different order compared to the above list
WebJul 22, 2010 · Simplest way to order a list is to use OrderBy List objListOrder = source.OrderBy (order => order.OrderDate).ToList (); If you want to order by multiple columns like following SQL Query. ORDER BY OrderDate, OrderId To achieve this you can use ThenBy like following. the future navigators guideWebApr 30, 2010 · Daniel is nearly right, but an easy way to use the order of the incoming list is to order by the index of the ID in that list: public List GetMyTypes (List ids) { return (from myType in db.MyTypes where ids.Contains (myType.Id) orderby ids.IndexOf (myType.Id) select myType).ToList (); } the future nathanielWebFeb 26, 2015 · You can use List.IndexOf and use int.Maxvalue if it returns -1: List result = list.OrderBy (str => { int index = comparer.IndexOf (str); return index == -1 ? int.MaxValue : index; }).ToList (); Here is a … the alchemist cocktail classWebDec 13, 2024 · If you want to order by nested elements, you could do it like this: query = query.OrderByDescending (x => { x.CourseProgresses = x.CourseProgresses.OrderByDescending (y => y.UpdateDate)); return x.Id; } Share Improve this answer Follow answered Dec 13, 2024 at 10:20 Sebastian Siemens 2,292 1 17 23 the alchemist code kanon j+the alchemist - cocktail barWebJan 2, 2024 · Here I'm merging a list to end of another list after sorting it according to another list.This should sort processdetails list according to the order in routeplans list if any item not present in processdetails list is found it should keep the order and add it to processdetails list. the alchemist cocktail makingWebFeb 23, 2024 · We now have the order of the letters that we want listB to reflect: First, the order given by listA, then, the remaning letters present in listB. The .Join() operation .Join() associates an outer sequence with an inner sequence based on association keys defined by a key selector for each sequence ; and then returns an IEnumerable from the ... the future network of intelligence institute