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: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)));
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));
Source: stackoverflow.com
Related Query
- 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
More Query from same tag
- How to assign List<Bear> or List<Goat> to List<IAnimal>
- Combine duplicates in a list
- Linq: Transforming specified column's datatypes and values while preserving unspecified columns
- LINQ Query on Huge List()
- LINQ GroupBy on multiple ref-type fields; Custom EqualityComparer
- How to discover financial Year based on current datetime?
- Correctly convert a string to dictionary with irregular csv in c#?
- Order Dictionary from Keys from another Dictionary
- Concatenation of IEnumerable collections C#
- Implement IEnumerable jagged array which returns transposed item
- Content of a tag in linq to xml
- Why doesn't my LINQ to Objects query return any results?
- How to read specific line from large file?
- LINQ Lambda Order in writing the query
- How to sort an IEnumerable<AnonymousType> or sort a two datatable join using LINQ?
- All items that each of them match all a collection keywords
- Why have a non-generic IQueryable?
- EF Code First: Relationships with differing key names
- How do I implement words that are not in dictionary must shown with error?
- How to select child nodes when parent node equals a specific value in LINQ to XML
- C# LINQ to rank time series values on a daily basis => partition by date, then rank by value (largest to smallest)
- Aggregate LINQ results
- Inverting a Hierarchy with Linq
- Object reference not set to an instance of an object. while Accessing Excel Sheet Cell Values
- C# - strange behaviour with ToList() and ToArray()
- LINQ to Entities method 'System.String ToString Error
- Linq FindWhere clause and boolean filters?
- Is there a better or more efficient way to filter with linq
- Entity Framework Include Parent Entities
- GroupBy LINQ statement