score:1
i'm not sure what the criteria for determining matches is, so i've written this - it's completely novel, it's a 99.9999% certainty that it's not actually what you want.
essentially, the outer select gets all the substrings of the determined length.
the first inner select determines the maximum length of this string that was found in at least one other string in the list.
the group by (following the first inner select) groups the found lengths by themselves.
this grouping is then converted to a dictionary of the length versus the number of times it was found.
we then order that set of groupings by frequency (value
) that the length was found (ascending).
next, we take that actual length (the least frequently occurring length - from key
) and spit it back out into the second parameter of substring
so we take the substrings from 0 to that length. of course, we're back in the outer select now, so we're actually getting values (hooray!).
now, we take the distinct set of values from that result and voila!
list.select(
item => item.substring(0,
list.select(
inneritem => enumerable.range(1, inneritem.length)
.reverse()
.first(proposedlength => list.count(innerinneritem => innerinneritem.startswith(inneritem.substring(0, proposedlength))) > 1)
)
.groupby(length => length)
.todictionary(grouping => grouping.key, grouping => grouping.count())
.orderby(pair => pair.value)
.select(pair => pair.key)
.first())
).distinct()
after reading the comments above, i see that there's also an interest in finding the distinct longest substrings present in any of the others for each term. here's more novel code for that:
list.select((item, index) => new {
index=index,
length=enumerable.range(1, item.length)
.reverse()
.first(proposedlength => list.count(inneritem => inneritem.startswith(item.substring(0, proposedlength))) > 1)
}).select(n => list[n.index].substring(0, n.length))
.distinct()
in short, iterate through each item in the list and collect the index of the entry and the longest substring from the beginning of that element that may be found in at least one other entry in the list. follow that by collecting all the substrings from each index/length pair and taking only the distinct set of strings.
score:0
solved! thanks to @mlorbetske and @shelleybutterfly
list.select((item, index) => new { index=index,
length=enumerable.range(1, (item.length-2)) //i don't need the last 2 char so i'm ignoring it
.reverse()
.first(proposedlength => list.count(inneritem =>
inneritem.startswith(item.substring(0, proposedlength))) >
1)}).select(n => list[n.index].substring(0, n.length)).distinct()
score:1
does it need to be inline query syntax? if so, how about:
var result =
from item in list
select item.substring(0,6);
or with the distinct requirement:
var result =
(
from item in list
select item.substring(0,6);
)
.distinct();
score:5
i doubt that this is what you're looking for, however
var result = list.select(s => s.substring(0, 6))
.distinct();
Source: stackoverflow.com
Related Query
- how to extract common part from a string list using C#
- How to create a comma delimited string from distinct list of values using LINQ?
- Extract list of objects from a string using linq
- How to extract the most common YEAR from an array of DateTime objects using LINQ
- To determine if one of the Strings from a list contains the initial part of a specified string using LINQ
- How to extract items at specified locations from list of comma separated strings using LINQ
- How do I get quoted fields from a delimited string as a list of unquoted values using LINQ?
- How to extract specific value from this json string using linq
- c# Linq or code to extract groups from a single list of source data
- How to get duplicate items from a list using LINQ?
- How do I get a distinct, ordered list of names from a DataTable using LINQ?
- How to select values within a provided index range from a List using LINQ
- How to replace some particular string in a list of type string using linq?
- How can I extract a list of Tuple from a specific table with Entity Framework / LINQ?
- How to remove characters from a string using LINQ
- Using LINQ to extract ints from a list of strings
- using LINQ how can i concatenate string properties from itesm in a collection
- Extract data from a XML string using linq vs xmlDocument
- how add the list of strings into string using linq?
- How to create a concatenated string from a List of Objects in VB.Net 3.5
- How to remove a List string element if it contains a string element from another List?
- How can I retrieve a set of unique arrays from a list of arrays using LINQ?
- How to generate a unique list of items from another list using LINQ in C#
- How to check/filter uppercase words alone from a string using C#?
- How to fill a property of an object from another list using LINQ
- Using LINQ how do I create a List of one particular field of an entity from a collection entities
- How might I take two random records from a list using Linq?
- How to get unique values from two list of dictionaries using Linq?
- LINQ DynamicLibrary: How to extract count and list from IQueryable
- How do I get an array of repeated characters from a string using LINQ?
More Query from same tag
- Getting error when fetching data from Sql server compact using linq
- Need a linq to objects query for a nested collection
- How can I reformat this to use the other kind of LINQ Query format?
- How to group multiple lists using LINQ - Sum and GroupBy
- GroupBy with different parameters
- InvalidOperationException Occurred in Linq orderby clause
- Find duplicates within multiple lists using linq
- OrderByDescending does not sort List<string> properly
- How can i use Linq / Linq to Sql to do this?
- linq issue with creating relationships
- C# Linq Expression - How to get the instance of an expression
- Sort list by object property with sort parameter
- Find all ids from list in another list
- Entity Framework - Eagerly load object graph using stored procedures
- Linq to SQL is user date range intersecting DB date range
- Checking for nulls in Linq Query
- Linq + foreach loop optimization
- Linq to get row with a minimum value of a column but based on another column value
- How to I read a json file containing array with C# and perform LINQ query on it?
- Why do I need Stored Procedures when I have LINQ to SQL
- MongoDB.Driver with Cosmos DB array searches
- Bad Storage property: unknown cause
- Populating a XAML Store App combobox from a Linq Query
- Parse object to a dynamically supplied type
- Linq query, return distinct on single field & returning subset of data
- Silverlight databinding error
- Can I use LINQ without Entity Framework?
- Do I need to nullcheck when I am using two nullable properties in my orderby?
- ordering of OrderBy, Where, Select in the Linq query
- List all highest datarow or spezific datarow