score:13
If you don't care which characters then you could just use the String constructor:
String s = new String('0', 500);
This will give you a string with 500 "0"s. Or for 500 X's:
String s = new String('X', 500);
score:1
This should do the trick.
var range = new string(String.Concat(Enumerable.Range(0, 500)
.Select(c => c.ToString()))
.Take(500).ToArray()
);
score:1
LINQ is nifty, but you don't always need it...:
static string NumString(int length) {
var s = "";
var i = 0;
while (s.Length < length) {
s += i.ToString();
i++;
}
return s.Substring(0, length);
}
Or a variant using Aggregate:
var str = Enumerable.Range(0, 500)
.Aggregate("", (s, next) => s += next.ToString(),
s => s.Substring(0, 500));
score:2
You want to use Aggregate:
string range = Enumerable.Range(0,500)
.Select(x => x.ToString()).Aggregate((a, b) => a + b);
Console.WriteLine(range);
This will give you a string of concatenated numbers from 0 to 500. Like this: 01234567891011121314151617...
if you need to take 500 chars from this big string, you can further use substring.
string range = Enumerable.Range(0,500)
.Select(x => x.ToString()).Aggregate((a, b) => a + b);
Console.WriteLine(range.Substring(0, 500));
score:0
new string(Enumerable.Range(0,500).SelectMany(x => x.ToString()).Take(500).ToArray())
Simplest way with LINQ. Since a string is technically an enumerable (char[]), you can use SelectMany (which takes multiple Enumerables and flattens into a single collection) followed by a Take(500) to only get 500 characters, call ToArray to get a char[] to instantiate a new string.
score:0
One more option, not quite the requested output from the original question - digits are random rather than increasing - but fairly succinct (I know, the OP didn't require a string of random digits but here's one in any case).
var rand = new Random();
string.Join("",Enumerable.Repeat(0, 500).Select(i => rand.Next(10)));
Source: stackoverflow.com
Related Articles
- Generate a fix sequence of strings with C#/Linq
- Generate number sequence with step size in linq
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- Generate number sequences with LINQ
- Getting odd/even part of a sequence with LINQ
- Use LINQ to group a sequence of numbers with no gaps
- Removing characters from strings with LINQ
- Make Linq to Sql generate T-SQL with ISNULL instead of COALESCE
- Generate sequence with step value
- Using LINQ to generate a random size collection filled with random numbers
- How to find a match with 2 comma separated strings with LINQ
- Dynamically generate LINQ select with nested properties
- Lazily partition sequence with LINQ
- Determine, that sequence contains other sequence, in same order, with using LINQ
- LINQ Source Code Available
- How does this linq code that splits a sequence work?
- Use LINQ to group a sequence by date with no gaps
- Linq with where clause in many-to-many EF Code First object
- How to concat strings in LINQ while properly dealing with NULL values
- Look for words in strings with LINQ
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- Conditional update strings in collection with LINQ
- Joining strings in a JArray with LINQ
- How could I use LINQ to filter out strings starting with a variety of sub-strings?
- Help with Linq expression to return a list of strings based on field count
- C# - Linq optimize code with List and Where clause
- Sequence contains no elements with LINQ FirstOrDefault
- creating Linq to sqlite dbml from DbLinq source code
- How can I generate all possible LINQ strings of a json object for Json.net?
- Stubbing Code for Test With Linq Expressions and Lambdas
- Concatenating a LINQ (To SQL) Query
- Clear multiple column values in DataTable
- Linq to XML - set Xelement value depending on if Statement
- How to join two customer lists into one list
- filtering a table by ids of another table in linq
- LINQ update question comparing to SQL update. need help
- Get SQL statement from a predicate?
- C# Cast object to IEnumerable<T> and use Linq extensions when the enumerable type is a value type e.g. enum
- ThenBy fails after Select
- LINQ: multiple levels
- C# LINQ to SQL Invalid Object Name
- Linq mystery error in EF query
- LINQ not executed correctly
- Linq expression to return the first overloaded method that takes 1 string parameter
- Relation from master to details but not vice versa
- Refactoring predicate out of the Lambda expressoin caueses an exception
- Extension method not updating object passed in
- LINQ Select syntax returns all columns instead of selected ones
- Group items and select specific item from each group with LINQ
- How to distinct groups of values?