简体   繁体   English

需要帮助来处理复杂的linq查询

[英]Need help for a complex linq query

Ok so I've got a DataTable here's the schema 好的,所以我有一个DataTable,这是架构

        DataTable dt = new DataTable();
        dt.Columns.Add("word", typeof(string));
        dt.Columns.Add("pronunciation", typeof(string));

The table is filled already and I'm trying to make a linq query so that i can output to the console or anywhere something like : 该表已被填充,我正在尝试进行linq查询,以便可以将其输出到控制台或诸如此类的任何地方:

   Pronunciation : akses9~R => (list of words)

I want to output the pronunciations the most common and all the words that use it. 我想输出最常见的发音以及所有使用它的单词。

Something like this should give you what you want: 这样的事情应该给你你想要的东西:

var results = dt.GroupBy(dr => dr.pronunciation);

foreach(var result in results)
{
    Console.Write("Pronunciation : {0} =>", result.Key);
    foreach(var word in result)
    {
        Console.Write("{0} ", word);
    }
    Console.WriteLine();
}

The GroupBy gives you an IGrouping whose Key property will contain the pronunciation and collection itself will contain all the words. GroupBy提供了一个IGrouping,其Key属性将包含发音,集合本身将包含所有单词。

Sounds like you want a group by: 听起来您想要一个分组依据:

var q =
    from row in dt.Rows.Cast<DataRow>()
    let val = new { Word = (string)row["word"], Pronunciation = (string)row["pronunciation"] }
    group val by val.Pronunciation into g
    select g;

foreach (var group in q)
{
    Console.WriteLine(
    "Pronunciation : {0} => ({1})", 
    group.Key, 
    String.Join(", ", group.Select(x => x.Word).ToArray()));
}
var words = from row in table
            where row.pronunciation == "akses9~R"
            select row.word;
foreach (string word in words)
{
    Console.WriteLine(word);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM