把这些写成一个linq查询?[英] Write this into one linq query?

本文是小编为大家收集整理的关于把这些写成一个linq查询?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这将其根源纳入我问的另一个问题,该问题解决了此问题的一部分 -

将此代码转换为linq?

现在,我试图以以下方式编写整个逻辑 -

var divisions = (
    from DataRow row in results.Rows
    let section = row["SECTION"].ToString()
    orderby section ascending
    select section).Distinct();

string result = String.Empty;

foreach (string div in divisions)
{
    result = String.Concat(result, div, Environment.NewLine);

    var query =
        from DataRow row in results.Rows
        let remarks = row["REMARKS"].ToString()
        let exam = row["EXAM_NAME"].ToString()
        let rollno = row["ROLL_NO"].ToString()
        let section = row["SECTION"].ToString()
        where (remarks == "Passes" || remarks == "Promoted") &&
            exam == "TOTAL" && section == div
        orderby rollno
        select rollno;

    result = String.Concat(result,string.Join(" ", query.ToArray()),
        Environment.NewLine);            
}

基本上,原始DataTable有一堆行,其中包含各种信息在内.我想创建一个单个字符串,为此,每个划分都会出现在新线上,在该分区的NOS下方以逗号分隔的方式显示.下一行的下一个分区,依此类推. (在这里部分和部门是可互操作的).

有什么优雅的方法可以用一个Linq查询编写此内容,而不必循环浏览第一个查询的结果?

编辑:

数据(不提到过滤条件下使用的其他列)

Roll_no Section.. other cols

001     A
002     A
001     B
003     A
004     B
006     B

这是输出的外观 - (滚动否仅在一个部门内是唯一的,但这不应以任何方式影响逻辑)

A
001 002 003
B
001 004 006

这就像'a \ r \ n001 002 003 \ r \ nb \ nb \ r \ r \ n001 004 006'当字符串为原始格式时.

注意,以上代码有效.我只是在寻找一种更好的方法.

推荐答案

您想实现两个单独的要求,并且您不应该尝试将它们合并为一件事情.您1.想对结果进行分组和2.有特定的演示需求.

这是您可以做到的:

var query = 
    from DataRow row in results.Rows
    // here the query stuff you already had
    select new { rollno, section, exam, remarks };

// 1. Grouping
var groups =
    from item in query
    group item by item.section into g
    select new
    {
        Section = g.Key,
        Rollnos = g.Select(i => i.rollno).ToArray(),
    };

// 2. Presentation
foreach (var group in groups)
{
    Console.WriteLine(group.Section);
    Console.WriteLine(string.Join(" ", group.Rollno));
}

可以编写一个也为您提供一部分演示文稿的单个查询,但是此查询将变得非常讨厌和不可读.

本文地址:https://www.itbaoku.cn/post/1557180.html

问题描述

This has its root into another question I asked, that solved a part of this problem -

Convert this code to LINQ?

Now I am trying to write the entire logic in the following manner -

var divisions = (
    from DataRow row in results.Rows
    let section = row["SECTION"].ToString()
    orderby section ascending
    select section).Distinct();

string result = String.Empty;

foreach (string div in divisions)
{
    result = String.Concat(result, div, Environment.NewLine);

    var query =
        from DataRow row in results.Rows
        let remarks = row["REMARKS"].ToString()
        let exam = row["EXAM_NAME"].ToString()
        let rollno = row["ROLL_NO"].ToString()
        let section = row["SECTION"].ToString()
        where (remarks == "Passes" || remarks == "Promoted") &&
            exam == "TOTAL" && section == div
        orderby rollno
        select rollno;

    result = String.Concat(result,string.Join(" ", query.ToArray()),
        Environment.NewLine);            
}

Basically, the original datatable has a bunch of rows with various information including Division. I want to create a single string, for which every division appears on a new line, and below that the roll nos for that division are shown in comma separated fashion. Next division on next line, and so on. (here Section and division are interoperable terms).

Is there any elegant way to write this with one linq query, instead of having to loop through the results of the first query?

EDIT:

Data (not mentioning the other columns that are used in filter conditions)

Roll_no Section.. other cols

001     A
002     A
001     B
003     A
004     B
006     B

This is what the output will look like - (roll no is unique only within a division, but that should not affect the logic in any way)

A
001 002 003
B
001 004 006

This will be like 'A\r\n001 002 003\r\nB\r\n001 004 006' when the string is in raw format.

Note, the above code works. I am just looking for a better approach.

推荐答案

There are two separate requirements you want to have implemented, and you should not try to merge them into a single thing. You 1. want to group the results togetter and 2. have specific needs for presentation.

Here is how you can do this:

var query = 
    from DataRow row in results.Rows
    // here the query stuff you already had
    select new { rollno, section, exam, remarks };

// 1. Grouping
var groups =
    from item in query
    group item by item.section into g
    select new
    {
        Section = g.Key,
        Rollnos = g.Select(i => i.rollno).ToArray(),
    };

// 2. Presentation
foreach (var group in groups)
{
    Console.WriteLine(group.Section);
    Console.WriteLine(string.Join(" ", group.Rollno));
}

It is possible to write one single query that also does part of the presentation for you, but this query would become very nasty and unreadable.