[英]How to Select Multiple Fields from DataGridView using LINQ in C# WinForms
技术细节:
我正在尝试使用 LINQ 从 datagridview 查询数据并将其显示在另一个 datagridview 中(原始数据源是 datagridview 在运行时读取的文本文件)。 到目前为止,我只能在选择一个字段时做到这一点。
例如:
//all pages hits report
var pageCountQuery = (dataGridViewIISDateTime.Rows.Cast<DataGridViewRow>()
.Where(r => r.Cells[9].Value != null)
.Select(r => r.Cells[9].Value)
.GroupBy(pg => pg)
.OrderByDescending(pg => pg.Count())
.Select(g => new { PAGE = g.Key, HITS = g.Count() })).ToList();
dataGridView1.DataSource = pageCountQuery;
和:
//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;
但是,当我尝试使用与上述相同的代码选择两个或三个字段时,我开始收到一些关于语法错误、变量超出范围等的警告。
以下是我正在尝试做的(SQL):
选择两个字段的示例:
//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
选择三个字段的示例:
//500 errors per page and user report
SELECT username, page, http
FROM [LogFileName]
WHERE http = 500
GROUP BY username, page
ORDER BY count(http) DESC
认为我可以翻译这个:
//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 被停用时
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.