需要帮助来配置LINQ查询

时间:2010-06-11 15:08:32

标签: linq

我正在构建一个单词anagram程序,它使用一个包含一个简单表的数据库:

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

alphagram是按字母顺序排列的单词的字母。例如,OVERFLOW的alphagram将是EFLOORVW。我数据库中的每个Alphagram都有一个或多个Anagrams。这是我的表的示例数据转储:

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

我正在尝试构建一个LINQ查询,该查询将返回Alphagrams列表及其关联的Anagrams。这可能吗?


更新:根据以下建议,这是我的解决方案!谢谢大家!

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

2 个答案:

答案 0 :(得分:1)

<击>

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

<击>

一个全新的答案......

您似乎需要查看How to: Group Data (Entity Framework)的分组查询。

这应该实现你想要的......

我用LINQ做了一个暴躁,这很有效......

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

答案 1 :(得分:0)

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

这是你在找什么?它是对象的LINQ。您可能希望使用LINQ-to-SQL或LINQ-to-Entites将记录提取到对象中,然后对已经获取的对象集合使用上述LINQ-to-Objects查询。