score:2
or if you want to use the c# language extensions:
var words = (from line in new[] { "one two", "three four five" }
from word in line.split(' ', stringsplitoptions.removeemptyentries)
select word).count();
score:1
not an answer to the question (that was to use linq to get the combined word count in the array), but to add related information, you can use strings.split and strings.join to do the same:
c#:
string[] stringarray = { "one two", "three four five" };
int numwords = strings.split(strings.join(stringarray)).length;
vb.net:
dim stringarray() as string = {"one two", "three four five"}
dim numwords as integer = split(join(stringarray)).length
score:5
i think sum is more readable:
var list = new string[] { "1", "2", "3 4 5" };
var count = list.sum(words => words.split().length);
score:6
you can do it with selectmany:
var stringarray = new[] {"one two", "three four five"};
var numwords = stringarray.selectmany(segment => segment.split(' ')).count();
selectmany flattens the resulting sequences into one sequence, and then it projects a whitespace split for each item of the string array...
Source: stackoverflow.com
Related Query
- How do you count the words in an array of strings with LINQ?
- How do you sort a String List by the number of words matched with an Array in Linq
- How to count the number of elements that match a condition with LINQ
- How can you do custom sorting in LINQ with null always on the end?
- How can you use LINQ to find Azure AD users with specific licenses using the Azure AD Graph API Client Library 2.0
- How to swap the data source associated with a Linq query?
- With LINQ how do you return a string const in the result from the select operator
- How to compare array of elements with the linq query result in c#
- In a many to many relationship , how to retrieve the values like 2-D array with help of linq
- How can you take only the last result with Linq
- How do I count the number of records with no match on the right side of a Linq left join?
- Using LINQ how do you filter a list of strings for values that match the pattern "q1, q2" etc.?
- How I get the Attributevalue from a Xml to a Array with Linq to XML?
- How Do You Return True If A String Contains Any Item In An Array Of String With LINQ in VB.NET
- How to check variable length strings to see if they start with anything in the array
- How do you perform a CROSS JOIN with LINQ to SQL?
- How to select only the records with the highest date in LINQ
- How do I get the MAX row with a GROUP BY in LINQ query?
- How do I get the max ID with Linq to Entity?
- How to Count Duplicates in List with LINQ
- How can you handle an IN sub-query with LINQ to SQL?
- How do you use LINQ to find the duplicate of a specific property?
- How do you use LINQ with Sqlite
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- How to insert a record with LINQ and C# and return the Primary Key of that record
- How do I count the number of child collection's items using LINQ Method Syntax?
- How to get the item before current and after current in a dictionary with Linq / C#?
- How would you implement LINQ methods with SelectMany?
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- How do I use a custom comparer with the Linq Distinct method?
More Query from same tag
- LINQ get the latest status
- LINQ in child AppDomain
- Dynamic Linq Query where
- Join into with column and variable value compared using Linq or Entity Framework queries
- Are there any performance costs of using JSLINQ
- How do you map LINQ-to-SQL to your BLL classes?
- How to use a dynamic LINQ join extension method for multiple conditions
- Best way to present the user the XML values of a file?
- VB Linq Group Join query FAILS to order the results in descending order
- Basic array Any() vs Length
- How to create a dictionary from a list with an object as key
- From SQL database to model using linq
- Deleting datatable data using results from linq query
- How to optimize and pre-compile this LINQ expression?
- How should one attach an XDocument to a MailMessage in C#?
- Successive LINQ queries
- linq how to add symbol to empty lines from csv
- C# Linq multi-word search
- Get count of a datatable column contains with specific value
- Server-side equivalent of HttpContext?
- C# - strange behaviour with ToList() and ToArray()
- Linq: List of lists to a long list
- C# linq similar range list
- Using ==Null in Linq not working to find null values
- Combine two list of decimals in one array
- Is it possible to get a single element from XMl with the lowest value?
- Alternative for LINQ's .Contains()
- What is meant by 'The specified LINQ expression contains references to queries that are associated with different contexts'
- how to Check for equality with list input in linq
- linq how to get values from var