首页 \ 问答 \ 区别使用条件Linq(Distinct Using Condition Linq)

区别使用条件Linq(Distinct Using Condition Linq)

所以我已经花了一些时间用这个linq并且无法提出最佳解决方案。 考虑以下linq

在此处输入图像描述

我希望能够根据BaseItemID值选择不同的条目,所以例如每当我有两个Chicken条目时,我想选择第二个条目,因为它取代了第一个条目。 短语的另一种方法是每当条目具有BaseItemID值时,选择此条目并从列表中删除基本条目,因此将返回列表并删除项目1003。 另一种表达条件的方法是仅选择BaseItemID字段中任何行中不存在其ID值的项目。

如果我可以按名称分组,然后选择具有BaseItemID!= null和Count> 1的项目,那么问题就很容易了,但事实并非如此,而且Name字段不是唯一的,我真的不能依赖于它用于分组。

谢谢


So I have been spending sometime with this linq and couldn't come up with an optimal solution. Considering the following linq

enter image description here

I want to be able to select the distinct entries based on the BaseItemID value, so for example whenever I have two Chicken entries, I want to select the second entry because it superseeds the first entry. Another way to phrase it is whenever an entry have a BaseItemID value, pick this entry and remove the base entry from the list, so the list would be returned with the Item 1003 removed. Yet another way to phrase the condition is to select only the items that doesnot have their ID value existing in any row in the BaseItemID field.

The problem would have been easy if I can group by the name for example, and then pick the item with the BaseItemID != null and Count > 1, but thats not the case and the Name field is not unique and i cant really depend on it for the grouping.

Thanks


原文:
更新时间:2022-05-23 18:05

最满意答案

如果要有一个显示每个用户的最新时间戳的列表,则应存储一个列表,该列表显示数据库中每个用户的最新时间戳。

LatestTimstampForUserID:
  - [UserID01]: [TimeStamp02]
  - [UserID02]: [TimeStamp04]
    ...

现在,您可以非常有效地从数据库中读取此数据并显示列表。

成本是,当为用户发布新消息时,您需要更新数据库中的多个位置。 这在使用NoSQL数据库(例如Firebase实时数据库)时非常常见。


If you want to have a list that shows the latest timestamp for each user, you should store a list that shows the latest timestamp for each user in your database.

LatestTimstampForUserID:
  - [UserID01]: [TimeStamp02]
  - [UserID02]: [TimeStamp04]
    ...

Now you can very efficiently read this data from the database and show the list.

The cost is that you'll need to update multiple locations in the database when a new message is posted for a user. This is very common when using NoSQL databases, such as the Firebase Realtime Database.

相关问答

更多
  • 我的问题是:如果要求我们对两个输入数组进行排序,我们从特殊处理中获得多少效率? 根据Array.Sort的文档, 平均为O(n log n) ,最坏情况下为O(n ^ 2) (quicksort)。 一旦我们对两个数组进行了排序,我们就会有另外的O(n)工作量,因为我们必须遍历第一个。 我将此解释为意味着由于排序所需的迭代次数,然后处理,总体工作量可能实际增加 。 如果您可以在开始时保证排序数组,那当然会是一个不同的故事,但正如您所说,您不能。 (我还应该注意,您需要创建一个自定义IComparer实 ...
  • 怎么样简单: 更新prism_actions a,prism_players p SET a.player_id = p.player_id其中a.player = p.player; 我不知道要快多少,但它会消除子查询。 What about a simple: UPDATE prism_actions a, prism_players p SET a.player_id = p.player_id where a.player=p.player; I don't know how much quicke ...
  • 您可以使用NoResource作为您希望在请求时返回404响应的资源: from twisted.web.resource import Resource, NoResource saml = NoResource() saml.putChild(b"login", ...) saml.putChild(b"logout", ...) ... root = Resource() root.putChild(b"saml", saml) You can use NoResource for a reso ...
  • 有可能为您的查询做到这一点,但由于最终目标是生成一个分层对象模型,所以您花费的时间和精力将花费在研究可自动执行大部分繁重工作的对象/关系映射器上。 有什么可用取决于客户端应用程序的编程语言; 维基百科列出了一些列表。 It's possible to do that to your queries, but since the final goal is to produce a hierarchical object model, your time and effort would be much be ...
  • 使每个col成为一个有序集,然后在每个键上使用ZRANGEBYSCORE ,并在应用程序中进行交集和计数。 我使用phpredis ,我使用array_intersect在内存中做了很多。 性能问题出现在ZADD ,您将使用它来创建有序集。 一旦你在Redis的内存中创建了所有已排序的集合,剩下的就非常快了。 创建有序集(Redis示例) ZADD col1 12.3 line1 ZADD col1 79.3 line2 ZADD col1 55.6 line3 ZADD col2 37.4 line1 ...
  • 使用面具。 要获得子网,您和掩码的地址。 如果使用多个地址执行此操作并使用最小的掩码,则相等的结果将全部成为较大子网的一部分。 例如: 10.11.12.13/32 10.11.12.12/31 10.11.12.12/30 10.11.12.8/29 10.11.12.0/28 10.11.12.0/27 10.11.12.0/26 10.11.12.0/25 10.11.12.0/24 使用最小的掩码( 255.255.255.0 )屏蔽每个地址,您将看到它们都在10.11.12.0/24的范围内 Us ...
  • 如果你的查询是:“specific UserId + RecordType和CreationDateTime range”,那么你自己回答: 创建GSI: hash = UserId_RecordType(1234_type1) range = CreationDateTime 通过这种方式,您可以获取RecordType用户的所有文档,按CreationDateTime排序 小心使用热键。 if your query is: "specific UserId+RecordType, and Creatio ...
  • 如果要有一个显示每个用户的最新时间戳的列表,则应存储一个列表,该列表显示数据库中每个用户的最新时间戳。 LatestTimstampForUserID: - [UserID01]: [TimeStamp02] - [UserID02]: [TimeStamp04] ... 现在,您可以非常有效地从数据库中读取此数据并显示列表。 成本是,当为用户发布新消息时,您需要更新数据库中的多个位置。 这在使用NoSQL数据库(例如Firebase实时数据库)时非常常见。 If you want to ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。