简体   繁体   English

如何在 C# WinForms 中使用 LINQ 从 DataGridView 中选择多个字段

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

Technical Details:技术细节:

  • Visual Studio 2017 (Community Edition) Visual Studio 2017(社区版)
  • LINQ LINQ
  • C# (WinForms) C# (WinForms)

I'm trying to use LINQ to query the data from a datagridview and display it in another datagridview (the original data source is a text file that is read by the datagridview at runtime).我正在尝试使用 LINQ 从 datagridview 查询数据并将其显示在另一个 datagridview 中(原始数据源是 datagridview 在运行时读取的文本文件)。 So far, I'm able to do that when selecting one field only.到目前为止,我只能在选择一个字段时做到这一点。

For example:例如:

            //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;

and:和:

                //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;

But when, I'm trying to select two or three fields using the same code as above, I start getting several warnings about syntax errors, variables being out of scope, etc.但是,当我尝试使用与上述相同的代码选择两个或三个字段时,我开始收到一些关于语法错误、变量超出范围等的警告。

The following is what I'm trying to do (SQL):以下是我正在尝试做的(SQL):

An example of selecting two fields:选择两个字段的示例:

//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

An example of selecting three fields:选择三个字段的示例:

//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

Think I can translate this one:认为我可以翻译这个:

//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

as作为

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();

You haven't said what column HTTP code is in.. But the second SQL you've posted has syntax errors and would only really work in MySQL, and even then only if ONLY_FULL_GROUP_BY is deactivated你还没有说 HTTP 代码在哪一列。但是你发布的第二个 SQL 有语法错误,只能在 MySQL 中真正工作,即使这样也只有在 ONLY_FULL_GROUP_BY 被停用时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM