如何在 C# WinForms 中使用 LINQ Select 来自 DataGridView 的多个字段

How to Select Multiple Fields from DataGridView using LINQ in C# WinForms

技术细节:

我正在尝试使用 LINQ 从一个 datagridview 中查询数据并将其显示在另一个 datagridview 中(原始数据源是一个文本文件,由 datagridview 在运行时读取)。到目前为止,我可以在 select 只输入一个字段时做到这一点。

例如:

和:

                //IPs generating traffic report
                var ipCountQuery = (dataGridViewIISDateTime.Rows.Cast<DataGridViewRow>()
                    .Where(r => r.Cells[5].Value != null)
                    .Select(r => r.Cells[5].Value)
                    .GroupBy(ip => ip)
                        .OrderByDescending(ip => ip.Count())
                        .Select(g => new { IP_ADDRESS = g.Key, VISITS = g.Count()})).ToList();

                dataGridView1.DataSource = ipCountQuery;

但是,当我尝试使用与上面相同的代码 select 两个或三个字段时,我开始收到关于语法错误、变量超出范围等的警告。

以下是我正在尝试执行的操作 (SQL):

selecting 两个字段的示例:

//all pages hits and the IPs hitting them report
select page, ip, count(page)
from [LogFileName]
group by page, ip
order by count(page) desc

我尝试了以下(错误的方法):

//var pagesIPCountQuery = (dataGridViewIISDateTime.Rows.Cast<DataGridViewRow>()
                    //.Where(r => r.Cells[5].Value != null && r.Cells[9].Value != null)
                    //.Select(r => r.Cells[5].Value, r.Cells[9].Value)
                    //.GroupBy(ip => ip, page => page)
                    //.OrderByDescending(ip => ip.Count(), page => page.Count())
                    //.Select(g => new { IP = g.Key, Count = g.Count() }, { Page = })).ToList();

                    //dataGridView1.DataSource = pagesIPCountQuery;

select三个字段的示例:

//500 errors per page and user report
SELECT username, page, http
FROM [LogFileName]
WHERE http = 500
GROUP BY username, page, http
ORDER BY count(http) DESC

我尝试了以下操作,但我在 HTTP_CODE 字段上得到了一个复选框,并且它没有通过错误 500 过滤结果:

var error500Query = (dataGridViewIISDateTime.Rows.Cast<DataGridViewRow>()
                        .Where(r => r.Cells[9].Value != null && r.Cells[2].Value != null && r.Cells[11].Value != null)
                        .Select(r => new { Page = r.Cells[9].Value, HTTP = r.Cells[2].Value.Equals("500"), Username = r.Cells[11].Value })
                        .GroupBy(usernamepagehttp => usernamepagehttp)
                        .OrderByDescending(g => g.Count())
                        .Select(g => new { USERNAME = g.Key.Username, PAGE = g.Key.Page, HTTP_CODE = g.Key.HTTP, HITS = g.Count() })).ToList();

                    dataGridView1.DataSource = error500Query;

觉得我可以翻译这个:

//all pages hits and the IPs hitting them report
select page, ip, count(page)
from [LogFileName]
group by page, ip
order by count(page) desc

作为

var pageCountQuery = (dataGridViewIISDateTime.Rows.Cast<DataGridViewRow>()
    .Where(r => r.Cells[9].Value != null && r.Cells[5].Value != null)
    .Select(r => new { Page = r.Cells[9].Value, IP = r.Cells[5].Value })
    .GroupBy(pageip => pageip)
    .OrderByDescending(g => g.Count())
    .Select(g => new { PAGE = g.Key.Page, IP = g.Key.IP, HITS = g.Count() })).ToList();

你还没有说 HTTP 代码在哪一列..但是你发布的第二个 SQL 有语法错误并且只能在 MySQL 中真正工作,即使这样也只有ONLY_FULL_GROUP_BY 已停用