Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What is the most efficient way of getting all the distinct items in order from a list?
I have a List<KeyValuePair<string, string>> that possibly has multiple repeating items in it and only want the unique values by order within the list.
For example,
name : Orange
actualname : Orango
name : Lime
fullname : Lime Lime
actualname : Limo
My expected output would be,
name
fullname
actualname
have tried with,
arrayKeys.Select(pair => pair.Key).Distinct().ToArray()
but not match with the order of my expected output
You can try
var distinctKeys = yourList.Select(x => x.Key).Distinct().ToList();
First, you select the key of each KeyValuePair and with Distinct() you create a distinct IEnumerable, which you then convert into a list.
Online demo: https://dotnetfiddle.net/N3GUui
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Method that accepts two Dal objects =>>
Method(DalFirst[] source1, DalSecond[] source2):
source1.Select(
src => new DataExmaple()
{
Id = src.Id,
...
AdditionalField = source2.Select(x => x.Field).ToString()
}
I get the output name as just type
System.Linq.Enumerable+SelectArrayIterator`2[....
With FirstOfDefault => it turns out, but the same values are everywhere.
There is no default string representation of a collection. You'd need to specify how you want the data to render as a string. For example, if you want each value separated by a comma (or any other delimiter) then you can join the collection values with String.Join:
AdditionalField = String.Join(",", source2.Select(x => x.Field))
The problem is this expression:
AdditionalField = source2.Select(x => x.Field).ToString()
At this point, source2 is still the entire DalSecond[] source2 array. You need to do something to map each source1 item to the specific matching source2 value(s). I could give you more, but there's not enough information in the question yet to infer what you really want to do here.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Here's an example of my code thus far.
#string.Join(", ", #piece.PieceThemes.Select(t => t.Theme.Title).ToArray())
I would like to order the array in descending order. I found that .OrderByDescending() exists, but I'm having trouble understanding how to use it.
Any help is appreciated!
you provide a lamda function to specify the property to sort by
#string.Join(", ", #piece.PieceThemes.Select(t => t.Theme.Title).OrderByDescending(x=> x).ToArray())
in this case x is a string and you can just sort by it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How to order numbers stored as string in db by OrderBy function
mytable.OrderBy(s => s.Year);
for ex =>
string Year= 2013/2014
string Year= 2017/2018
You can use the following to take the first year only:
s.Year.Trim().Substring(0, 4)
If the values are in a character column then they will sort as characters, not as numbers.
The only real workaround I know of would be to
Either change that column to numeric data type, which you can't do if
the values are '2013/2014'
Create another numeric column that stores the numbers (i.e. 2013,
2017) and then sort on that column.
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 6 years ago.
Improve this question
I need sort list of string values using IComparable:
10.1, 10.10, 10.11, 10.2, 10.3
Expected result:
10.1, 10.2, 10.3, 10.10, 10.11
Tried to use list.Sort but didn't work
Basically what you want to do is to sort by the number after the decimal point in the string. So, take only that part, convert it to a number and then sort. Using Comparer it will look like
List<string> values = new List<string> { "10.1", "10.10", "10.11", "10.2", "10.3" };
values.Sort((x, y) => int.Parse(x.Split('.')[1]).CompareTo(int.Parse(y.Split('.')[1])));
Or using linq it will look like:
var result = values.OrderBy(value => int.Parse(value.Split('.')[1]));
In the case you want to first sort by the second part and then by the first you can do:
var result = values.OrderBy(value => int.Parse(value.Split('.')[0]))
.ThenBy(value => int.Parse(value.Split('.')[1]))
.ToList();
keep in mind that this solution is naive and assumes all your values have a . and that the 2 parts of it can be parsed into int - If it is not the case then you can use TryParse
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a field which is that consists from delimiter (dynamic number) parts:
(c44 EE0),(c2 EE7),(e79 EE10),(c2 EE90)
my field is a comma delimiter field and how can i split it (in Linq) and search in the parts separately?
i.e. query : get those parts which have C2
please let me describe my problem again i have a field in sql server which i want to get result of that via LINQ ,
above field named code in SNP table as you can see this code field is tab delimited so
i would like to catch those records which have C2 in their at least one part.
Its simple to split a field via 'comma delimiter' but how can i do this in a linq query?
var field = "(c44 EE0),(c2 EE7),(e79 EE10),(c2 EE90)";
var result = field.Split(',').Where(x => x.Contains("c2"));