foreach loop with 2 hashset variables c# [closed] - c#

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

Related

How to map IEnumerable<string> convert to string? [closed]

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.

C# How to sort list of objects with string values [closed]

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

c# - Check if object was converted when put into a list? [closed]

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?

List array of array of struct [closed]

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
First, I declared two structure like the following:
struct m1
{
int
int
char
...
}
and
struct m2
{
int
int
char
...
}
I have two arrays of m1 and m2. I need to map one m1 against to multiple m2. I don't Know the size of both, so after I search I decide to use
List<m1 or m2> = new List<m1 or m2>();
and works fine.
but
List<m1 , m2> = new List<m1 , m2>();
gives me error "require one type argument"
Please help me, how can I do that?
Thanks
List<T> requires one Type it cannot have two types ,what I see is you need to map one m1 against multiple m2, so in this case Dictionary<TKey,TValue> is suitable here:
Dictionary<m1 , List<m2>> = new Dictionary<m1 , List<m2>>();
and now add items in dictionary.

Is it safe to remove an entry from a Hashtable while iterating? [closed]

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
In other words, is this safe?
Hashtable myDict = ... //fill with some data
foreach (DictionaryEntry pair in myDict)
{
if (someCondition)
myDict.Remove(pair.Key);
}
It doesnt seem to have any side effects in my application so far.
edit: i have to mention that in my specific case, this dictionary contains only 1 entry.
edit2: sorry, the datatype is a Hashtable not a Dictionary.
No it is not safe, the contract of IEnumerator specifies that MoveNext should throw an exception if the underlying collection was modified after the enumerator was created.
No, it is not safe. with every removed entry it will reduce index no and will move to next element with increasing current index no, so it will create problem there.
#Lee gave you the answer.
It would be safe this way:
var toDelete = myDict.Where(someCondition).ToArray();
foreach (DictionaryEntry pair in toDelete)
myDict.Remove(pair.Key);

Categories

Resources