Iterate over collections union [closed] - c#

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have 2 instances of IEnumerable<string> and need to iterate over both of them. Is it possible to iterate within single foreach?

Have a look at Enumerable.Concat(). I think it will do what you want:
foreach (string str in Enumerable.Concat(collection1, collection2))
{
}
Or
foreach (string str in collection1.Concat(collection2))
{
}

Union will not work unless you want to iterate over the set union of the two IEnumerable. The set union is defined as the unification of both, without duplicates. So if you rely on iterating over duplicates, it will not work, you'll have to use Concat for that.

Related

Fill array starting from negative number [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
The array consists of 100 items. I need to initialize elements of the array which sequential numbers starting from -2.
You mean SomeArray[-2]? It's not possible, as a value inside [ ] brackets are indexes, and it they can't be negative numbers (but the index starts from 0)... It's like having minus 2nd apple in your basket.
But if you mean values, you can do it easily by a loop
for (i=0; i<=100; i++) {
SomeArray[i] = i-2;
}

How can I get from a string of a directory and file name only the file name? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
The code:
'Files' is a List<string> and _indx is an int.
label22.Text = files[_indx];
For example in 'files' in index[0] I have this string:
D:\New folder (45)\converted.avi_Automatic\Lightning 0 Length 2 [91 - 93]\000091.bmp
But instead in label22.Text I want it to show me only '000091.bmp' without the rest of the directory path.
How can I do it ?
Use Path.GetFileName:
label22.Text = Path.GetFileName(files[_indx]);
I believe you are looking for Path.GetFileName():
label22.Text = Path.GetFileName(files[_indx]);
Path.GetFileName(fileName) returns the file name without the directory.
taken from http://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.100).aspx
The simplest way is
Path.GetFileName(files[_indx]);

Null exception when trying to use a list [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have StartingMap that inherits from Map. Here is what I am trying to do:
Map m_map;
List<Map> m_versions;
m_versions.add(new StartingMap(...)); // create null reference exeption
m_map= new StartingMap(...); // no error and load the map perfectly
Why do I get an error with the first one and not the second one ? I am doing the same thing.
You must instantiate m_versions, like
m_versions = new List<Map>();
you need to initialize the m_versions:
m_versions = new List<Map>();
before you can use it and add items to it.
You need to instantiate List before you add any items to the collection. In the second example you are just calling the constructor of StartingMap completely different things.
So before you can add any items to your list you need to:
m_versions = new List<Map>()

explicitly casting generics in C# 4 [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
Can anyone help me in casting generic collection in c# 4.
Here is the code snippet.
GridView1.DataSource = dataServiceColl.Select(t => t.product_desc="EdibileItem")
It is throwing up runtime error at the below line,
Gridview1.Databind();
Saying it is a HTTP Exception.
I think it should be a simple type cast.
Thanks,
Kris.
Use
t => t.product_desc=="EdibileItem"
HTTP Exception? That has nothing to do with casting.
More importantly, why are you assigning "EdibleItem" to t.product_desc here?
Select(t => t.product_desc="EdibileItem")
Did you meant == instead of =? If so, would a Where be more appropriate than a Select?
I think it all boils down to: what are you trying to achieve, exactly?

How to form this Regex [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Suppose the string is:
string item = "t-ewrwerwerwerwer\r-rr\wrjkwlr";
I want to Replace all - except when it is preceded by r.
So resut will be
string cleanItem = "tewrwerwerwerwer\r-rr\wrjkwlr"'
What regular expression can be used?
I think this regular expression is a little more efficient:
-(?<!r-)
Or if your language doesn’t support negative look-behind assertions, use this expression:
(^|[^r])-
and replace it by \1 (first matching group).
A replacement on (?<!r)- by an empty string should do the trick I think.
(?<!r)-
As long as your regex flavor supports zero-width look-behind, that is.

Categories