tags:

views:

101

answers:

2

Can I do either of these using LINQ:

1. Check that each element in an IEnumerable<string> has the correct extension. If not, throw exception.

foreach(var filepath in filepaths)
    if(Path.GetExtension(filepath) != @".xml")
        throw new ArgumentException(...);


2. Take an IEnumerable<string> and serialise all of its elements into one string, with spaces in between each.

string args = "";
foreach (var filepath in filepaths)
    args += filepath + " ";

Thanks

+7  A: 
if (!filepaths.All(x => Path.GetExtension(x) == @".xml"))
{
  throw error;
}

string.Join(" ", filepaths.ToArray()) for the second question.

leppie
I like this better of the two only because All and "==" seems more readable than Any and "!=", but both will do just fine.
danijels
This is a trivial point, but *technically* speaking, this has a functional difference with the OP's code: it will throw if the sequence is empty.
Ani
@Ani you're right, and I'd even say it's not necessarily that trivial either.
danijels
@Ani: You talking about the string.Join? I did not write that, someone edited my answer. .NET 4 supports passing an IEnumerable to string.Join which would not exhibit that behaviour.
leppie
@Ani: An for `All`, it will return `true` if the sequence is empty.
leppie
@leppie: No, about the lack of equivalence of `!All(predicate)` and `Any(!predicate)` . The OP's code can be written as the second, but not the first.
Ani
@leppie: Yes exactly, which is what introduces the difference with the OP's code.
Ani
@Ani: How? It is exactly the same. His code will also not throw an exception if the sequence is empty.
leppie
@Ani: His won't, yours will. Think about it.
Ani
@Ani: I think you are mistaken :) `new object[0].All(x => false)` returns `true`. Just look at the code in reflector, it is pretty much identical to the OP's.
leppie
@Ani: LOL, you replied to yourself ;P
leppie
@leppie: Haha yes, you are right on both counts. I'm afraid the double negations have messed with my head.
Ani
On another note, I think we should delete this 'junk' comment thread, especially since my completely incorrect comment at the top has been upvoted.
Ani
@Ani: Why? I, and I am sure other people, make this mistake all the time :)
leppie
Yes, but the 'good stuff' will be hidden and the upvoted comment will be at the top. I could write a comment that summarized my mistake so that others will see it. Never mind, your choice.
Ani
Summary - I incorrectly stated that `!All(predicate)` and `Any(!predicate)` would behave differently for empty sequences, but @leppie correctly informed me that this is not the case.
Ani
+5  A: 

1.

if(filePaths.Any(filepath => Path.GetExtension(filepath) != @".xml"))
     throw new ArgumentException(...);

2.

string args = string.Join(" ", filePaths.ToArray());

or, with LINQ (much more inefficient):

string args = filePaths.Aggregate("", (combined, path) => combined + " " + path);
Ani
Inefficient? Do you have the numbers to back it?
bzlm
Linq will be inefficient as string type is immutable and concatenating all the instances using Aggregate will perform poorly in comparison.
danijels