将此写入一个linq查询吗? [英] Write this into one linq query?

查看:76
本文介绍了将此写入一个linq查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这源于我提出的另一个问题,它解决了部分问题-

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

是否将此代码转换为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).

有没有一种优雅的方法可以用一个linq查询编写此代码,而不必遍历第一个查询的结果?

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

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

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

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

推荐答案

您要实现两个单独的要求,并且不应尝试将它们合并为一件事情.您1.希望将结果汇总到一起,并且2.有特定的演示需求.

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.

这篇关于将此写入一个linq查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆