把这些写成一个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);            
}

基本上,原始数据表有一堆包含各种信息的行,包括除法.我想创建一个字符串,每个分区都出现在一个新行上,并且在该分区的下方以逗号分隔的方式显示该分区的卷号.下一行的下一个分区,依此类推.(这里的 Section 和 Division 是可互操作的术语).

有没有什么优雅的方法可以用一个 linq 查询来编写它,而不必遍历第一个查询的结果?

编辑:

数据(未提及过滤条件中使用的其他列)

Roll_no Section.. other cols

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

这就是输出的样子——(roll no 仅在一个部门内是唯一的,但这不应该以任何方式影响逻辑)

A
001 002 003
B
001 004 006

当字符串为原始格式时,这将类似于 'A\r\n001 002 003\r\nB\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.