LINQ 获取最新条目

发布于 2024-11-05 21:23:06 字数 2231 浏览 0 评论 0原文

我对 LINQ 相当陌生,所以这对你们中的一些人来说可能是一个简单的解决方案。我已经扯头发有一段时间了:)

场景: 我有一个包含 3 个具有不同端口的硬件模块的盒子。 使用串行通信,我将模块中的数据提取到名为 CurrentData_Tables 的 SQL 表中 因此,每次生产一个单位时,我的程序都会在表中写入一个条目,说明总和有多大。 让我尝试解释一下这些数据。

BoxID, ModuleID, PortNumber, Value,       Time
1,     0A,       1,          {Value},     {Date}

该特定模块的读数的时间完全相同,因此我想我可以按恒定的数据进行分组,然后获取最新的数据。 这就是我到目前为止所得到的:

SQLdbDataContext sqlDB = new SQLdbDataContext();
BindingSource bs = new BindingSource();
var Latest = from q in sqlDB.CurrentData_Tabels
             group q by new
             {
                 q.BoxID,
                 q.ModuleID,
                 q.PortNumber,
                 q.Time
             }
             into groupOrder
             select new
             {
                 BoxID = groupOrder.Key.BoxID,
                 ModuleID = groupOrder.Key.ModuleID,
                 Portnumber = groupOrder.Key.PortNumber,
                 Time = groupOrder.Key.Time
             };

这很好,因为我得到了我想要的所有结果,甚至更多。我不需要知道模块 0A、端口 1 上的读数在最新条目 3 之前已计数到 2。 这有道理吗?如果没有,请让我尝试再次解释。

做一个>获得最大的总和是行不通的,因为我也有触发布尔值的状态。 1 表示机器上存在警报,0 表示一切正常。

我很抱歉双重帖子。无论出于何种原因,我的临时帐户被锁定,我无法对我的旧帖子发表评论。现在我已经注册了,希望能够提供更多信息。

@Jon Skeet,这是模块 0B 上有 2 个活动端口的设置,想象一下,模块 0B 端口 1 上有一个活动警报,那么即使问题已解决,我仍然会得到显示的过时数据。

https://i.sstatic.net/oX1ZK.jpg

这是我的客户端的样子侧面:

https://i.sstatic.net/stBXY.jpg

显然我只想显示最新数据。我希望这能消除困惑。


我似乎已经解决了自己的问题,但效率不高。

SQLdbDataContext sqlDB = new SQLdbDataContext();

                    var LatestObjects = (from q in sqlDB.CurrentData_Tabels select q).OrderByDescending(x => x.Time).First();

                    var selectedobjects = from q in sqlDB.CurrentData_Tabels
                                          where q.Time == LatestObjects.Time
                                          select q;


                    dgvLiveData.DataSource = selectedobjects;
                    dgvLiveData.RowHeadersVisible = false;  

通过组合 2 个查询,我得到了我想要的。我确信这个问题可以通过一个查询来解决,但我距离成为一名能够解决这个问题的铁杆程序员还差得很远。

I'm fairly new to LINQ, so this might be an easy peasy solution for some of you. I've been ripping my hair out for quite a while now :)

Scenario:
I have a box containing 3 Hardware Modules that have diffrent ports.
Using Serial comunication I extract data from the modules into a SQL table called CurrentData_Tables
So each time a unit gets produced, my program will write an entry into the table saying how big the sum is.
Let me try and explain the data.

BoxID, ModuleID, PortNumber, Value,       Time
1,     0A,       1,          {Value},     {Date}

The time is excatly the same on the readings for that specific module, so I imagined I could group by the data that is constant and then just get the latest.
This is what I have so far:

SQLdbDataContext sqlDB = new SQLdbDataContext();
BindingSource bs = new BindingSource();
var Latest = from q in sqlDB.CurrentData_Tabels
             group q by new
             {
                 q.BoxID,
                 q.ModuleID,
                 q.PortNumber,
                 q.Time
             }
             into groupOrder
             select new
             {
                 BoxID = groupOrder.Key.BoxID,
                 ModuleID = groupOrder.Key.ModuleID,
                 Portnumber = groupOrder.Key.PortNumber,
                 Time = groupOrder.Key.Time
             };

And that's perfectly fine because I get all the results I want and more. I don't need to know that the reading on module 0A, port 1 had counted to 2 before the latest entry of 3.
Does that make sense? If not please let me try to explain it again.

Doing a > to get the largest sum wouldn't work since I also have statuses that trigger on bool values. One means there's an alarm present on the machine and zero means all is OK.

I'm sorry for the double post. For whatever reason my temporary account was locked out and I couldn't comment on my old post. Now I'm registered and should hopefully be able to provide additional information.

@Jon Skeet, Here's the setup with 2 active ports on Module 0B, Imagine that there had been an alarm active on module 0B port 1, then i would still get the outdated data shown even though the problem had been resolved.

https://i.sstatic.net/oX1ZK.jpg

Here's how it would look at my client side:

https://i.sstatic.net/stBXY.jpg

Obviously i would only like to display the latest Data. I hope this clears up the confusion.


I seem to have resolved my own issue but in a nonefficient way.

SQLdbDataContext sqlDB = new SQLdbDataContext();

                    var LatestObjects = (from q in sqlDB.CurrentData_Tabels select q).OrderByDescending(x => x.Time).First();

                    var selectedobjects = from q in sqlDB.CurrentData_Tabels
                                          where q.Time == LatestObjects.Time
                                          select q;


                    dgvLiveData.DataSource = selectedobjects;
                    dgvLiveData.RowHeadersVisible = false;  

By combining 2 queries I got what I wanted. I'm sure this could be resolved using one query but I'm nowhere near being a hardcore programmer that would figure that one out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2024-11-12 21:23:06

你在那里所做的事情是正确的。您可以通过在代码中将latestquery 的语句作为子查询将其写入一个查询中,但这没有任何不同。当您在代码中将查询设置为LatestQuery 时,不会对服务器执行任何操作。实际的获取是在使用查询中的数据时完成的。

例子:

  var q = dc.Mytable.Where(e=>e.id > 5);    // q is IQueryable, no fetch to db is executed yet
  q = q.Where(e=> e.name.StartWith("a"));   // q is still IQueryable and no fetch yet too
  var list = q.List();   // Fetching of data to the db happens here

What you have done there is correct. You can write it in one query by making the statement for latestquery as subquery in the code, but it doesnt make any different. When you set the query to LatestQuery in your code, no execution is made to the server. The actual fetching is done when the data in the query is used.

Example:

  var q = dc.Mytable.Where(e=>e.id > 5);    // q is IQueryable, no fetch to db is executed yet
  q = q.Where(e=> e.name.StartWith("a"));   // q is still IQueryable and no fetch yet too
  var list = q.List();   // Fetching of data to the db happens here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文