score:1

Accepted answer

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();

Related Query

More Query from same tag