开发者

Need help with formulating LINQ query

I'm building a word anagram program that uses a database which contains one simple t开发者_JAVA百科able:

Words
---------------------
varchar(15) alphagram
varchar(15) anagram
(other fields omitted for brevity)

An alphagram is the letters of a word arranged in alphabetical order. For example, the alphagram for OVERFLOW would be EFLOORVW. Every Alphagram in my database has one or more Anagrams. Here's a sample data dump of my table:

Alphagram   Anagram  
EINORST     NORITES
EINORST     OESTRIN
EINORST     ORIENTS
EINORST     STONIER
ADEINRT     ANTIRED
ADEINRT     DETRAIN
ADEINRT     TRAINED

I'm trying to build a LINQ query that would return a list of Alphagrams along with their associated Anagrams. Is this possible?


UPDATE: Here's my solution based on the suggestions below! Thanks all!

using (LexiconEntities ctx = new LexiconEntities())
{
    var words = ctx.words;

    var query =
        from word in words
        where word.alphagram == "AEINRST"
        group word by word.alphagram into alphagramGroup
        select new { Alphagram = alphagramGroup.Key, Anagrams = alphagramGroup };

    foreach (var alphagramGroup in query)
    {
        Console.WriteLine("Alphagram: {0}", alphagramGroup.Alphagram);
        foreach (var anagram in alphagramGroup.Anagrams)
        {
            Console.WriteLine("Anagram: {0}", anagram.word1);
        }
    }
}


var list = anagrams.Select( 
    a => new {  
           Alphagram = a.ToCharArray().OrderBy(s => s).ToString(),  
           Anagram = a  
       }).toList(); 

A totally new answer...

You seem to need a groupby query look at How to: Group Data (Entity Framework).

this should accomplish what you want...

I did a testy with LINQ and this works...

    var words = new List<Word>() 
    { 
        new ConsoleApplication1.Word("EINORST", "NORITES"), 
        new ConsoleApplication1.Word("EINORST", "OESTRIN"), 
        new ConsoleApplication1.Word("EINORST", "STONIER"), 
        new ConsoleApplication1.Word("ADEINRT", "ANTIRED"), 
        new ConsoleApplication1.Word("ADEINRT", "DETRAIN"), 
        new ConsoleApplication1.Word("ADEINRT", "TRAINED")
    };

    var q = words.GroupBy(w => w.Alphagram).Select(w => new { Alphagram = w.Key, Anagrams = w.Select(p => p.Anagram).ToList() }).ToList();

    foreach (var item in q)
    {
        Console.WriteLine("Alphagram : {0}, Anagrams = {1}", item.Alphagram, String.Join(",", item.Anagrams));
    }


    var words = new List<Words>() 
    { 
        new Words("EINORST", "NORITES"), 
        new Words("EINORST", "OESTRIN"), 
        new Words("EINORST", "STONIER"), 
        new Words("ADEINRT", "ANTIRED"), 
        new Words("ADEINRT", "DETRAIN"), 
        new Words("ADEINRT", "TRAINED")
    };       


    var result = words.GroupBy(w => w.Alphagram, w => w.Anagram)
                      .Select(w => new { 
                                            Alphagram = w.Key, 
                                            Anagrams = w.Where(p => w.Key.ToCharArray().SequenceEqualUnOrdered(p.ToCharArray())).ToList() 
                                       } 
                             )
                      .ToList();


public static bool SequenceEqualUnOrdered<T>(this IEnumerable<T> first, IEnumerable<T> second)
{
    return new HashSet<T>(first).SetEquals(second);
}

Is it what you are looking for? It is LINQ to Objects. You may want to use LINQ-to-SQL or LINQ-to-Entites to fetch your records into your objects and then use the above-mentioned LINQ-to-Objects query over the already-fetched object collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜