score:5
Accepted answer
this should do the trick:
var random = new random();
var list = new list<string> {
"a0.png",
"b0.png",
"b1.png",
"b2.png",
"b3.png",
"c0.png",
"c1.png",
"d0.png",
"d1.png",
"d2.png"
};
var startingchar = "d";
var filteredlist = list.where(s => s.startswith(startingchar)).tolist();
console.writeline(filteredlist.count);
int index = random.next(filteredlist.count);
console.writeline(index);
var font = filteredlist[index];
console.writeline(font);
but the problem with the entire solution is that the smaller the resulting filtered list is the less likely you are to get really random values. the random
class works much better on much larger constraints - so just keep that in mind.
score:4
random random = ...;
var itemsstartingwithc = input
.where(x => x.startswith("c"))
.tolist();
var randomitemstartingwithc =
itemsstartingwithc.elementat(random.next(0, itemsstartingwithc.count()));
the call to tolist
isn't strictly necessary, but results in faster code in this instance. without it, count()
will fully enumerate and elementat
will need to enumerate to the randomly selected index.
Source: stackoverflow.com
Related Query
- Select a matching string randomly from a list using LINQ
- How to select values within a provided index range from a List using LINQ
- Get indexes of all matching values from list using Linq
- Select distinct values from a list using LINQ in C#
- Select items from a List where the children contain the items from another List using LINQ
- using linq select from list data where
- Make a List of String array from a List of String using LINQ
- How to select multiple columns from dataset into a string list with LinQ
- LINQ Select Last & Unique Record from a DB using List
- Optimized way to select items from a Collection excluding a list of certain member using Linq
- Extract list of objects from a string using linq
- To determine if one of the Strings from a list contains the initial part of a specified string using LINQ
- Select from multiple list using LINQ
- C# - How to achieve multiple matching from a single string by using Linq and Regex?
- Select distinct values from a list using LINQ
- Formatting a string using values from a generic list by LINQ
- Update List from another List with Matching values using Linq
- Not able to select values from right list using LINQ join
- Select objects from a list using LINQ based on a property
- Get Distinct List of string from List of List string using Linq
- Using Linq to select from a List in a List with Contains
- Linq select Objects from List which have empty String
- Linq select from database table where matching property from List
- Select distinct int from 2 possible fields in list using linq
- How to select specific fields from dynamic list using LINQ
- Select single column as list from existing list using fieldname as string
- Find matching tag items in table from string array using linq
- Using a Linq query to select objects where any value of a property matches values from a list
- select unique list from list<string> using linq and substring c#
- C# Return true if string is different from any of the string in a list of strings using LINQ
More Query from same tag
- How to use where condition
- Fill in property in list from another list by joining them
- Populate class instance by enum using linq
- Parsing xml with inner nodes
- LINQ expression String.Contains doesn't work
- Comparing against child property in generic/dynamic linq predicate with reflection
- How to reconstruct linq expression tree
- Sometimes get Timeout expired exception with Linq to EF and after a restart of server, it works fine
- Querying DocumentDB using a property other than Id
- How to get moving combination from two List<String> in C#?
- Group & Aggregate multiple columns using LINQ
- Use of Unassigned Local Variable Error C# and LINQ
- Linq Query for most occuring property of a class
- Getting property names of a class using expressions, static methods, and a base object
- Passing objects in the linq statement
- C# - Check if the RESULT returned by a linq query on a DataTable has rows
- How can I force LINQ to SQL to perform an INNER JOIN on a nullable foreign key?
- Linq - Checking for all but only if there is at least one
- SQL to LINQ with multiple joins and group by
- How do you trim a string that might be null in Linq?
- How do I compare two lists where one list starts with the other, and only return the "overhang" elements?
- How to OrderBy by related entity in EntityFramework
- Iterating through IQueryable with foreach results in an out of memory exception
- Remove sub IEnumerable from IEnumerable
- LINQ: How to join on only a single row with the MAX ID of the join column, and not all rows?
- Databind ListView datasource and joined tables
- How I get RowNumber with Model In Entity Framework core
- skip multiple values in foreach loop
- Facing issue in updating large number of records in entity
- How to check the existence of Attribute in XDocument using LINQ to XML in C#