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.
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 last month.
Improve this question
I have the following
variableAs = "A12,B12,C12"
variableBs = "1.54,75.30,55.50"
method (HashSet<string> variableAs, HashSet<double> variableBs)
foreach (var variableA in variableAs)
{
Method here requires the two values, must have preserved datatype
and be in same order, ie A12 with 1.54, B12 with 75.30
}
I have tried zip from this answer but I do not know how it works with hashset arrays,
NOTE the Method i am editing has it has hashset, the actual values are for example only, if there is an error, It must be my understanding of what a hashset is but I cannot change the hashset
You need to first populate your input-strings into some collection:
var As = variableAs.Split(',');
var Bs = variableBs.Split(',');
However a HashSet is not an ordered collection and thus the wrong choice here. You'd need a List or just an array.
Now you may use the mentioned Zip-function to combine the two elements together:
var result = As.Zip(Bs, (a, b) => new { a, b });
See the complete code at this fiddle: https://dotnetfiddle.net/9qTG2E
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
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 7 years ago.
Improve this question
I am making a game where there are squares (sectors) generated to make a path for a ball to go. There are two types of sectors: Sector and Presector. They are all put into a list of type Sector. How would I check to see if a specific sector in that list was actually a Presector before it was put in?
BTW: Presector is a child class of Sector.
I looked all over the place and couldn't find anything. The as keyword isn't working for me, and Type.IsAssignableFrom isn't either. EDIT: is will not work either, since that just checks if an object is that type.
SAMPLE CODE TIME!
List<Sector> sectors = new List<Sector>();
sectors.Add(new Sector());
sectors.Add(new Presector());
Now, we have a list full of two sectors. The second one was casted. How do I find that out using code?
if (objectFromList is Presector)
// Code here..
List<Sector> sectors = new List<Sector>();
sectors.Add(new Sector());
sectors.Add(new Presector());
sectors.Add(new Sector());
Presector ps = new Presector();
sectors.Add(ps);
// this returns an array with one element
var x = sectors.OfType<Presector>().ToArray();
// this returns true (the second element IS a Presector)
var hasPresector = sectors.Any(s => s is Presector);
// this returns true (the presector is present in the list)
var containsPs = sectors.Contains(ps);
What's the problem with the 'is' keyword?
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 7 years ago.
Improve this question
I need to trim the trailing spaces at the start and end in this int array and how can I do it, also I am not getting Trim() function anywhere here. Pls suggest me.
int[] arrPCT = dtOld.AsEnumerable().Select(r => r.Field<int>("PCT")).ToArray();
int are numeric values. They don't contain spaces (or any other char). It makes no sense trying to trim them.
int[] arrPCT = dtOld.AsEnumerable().Select(r => r.Field<int>("PCT").Trim()).ToArray();
If r.Field("PCT") value is of string type.