区别使用条件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
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
原文:
最满意答案
如果要有一个显示每个用户的最新时间戳的列表,则应存储一个列表,该列表显示数据库中每个用户的最新时间戳。
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.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
我的问题是:如果要求我们对两个输入数组进行排序,我们从特殊处理中获得多少效率? 根据Array.Sort的文档, 平均为O(n log n) ,最坏情况下为O(n ^ 2) (quicksort)。 一旦我们对两个数组进行了排序,我们就会有另外的O(n)工作量,因为我们必须遍历第一个。 我将此解释为意味着由于排序所需的迭代次数,然后处理,总体工作量可能实际增加 。 如果您可以在开始时保证排序数组,那当然会是一个不同的故事,但正如您所说,您不能。 (我还应该注意,您需要创建一个自定义IComparer
实 ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
怎么样简单: 更新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 ...
-
扭曲的子女(Twisted subchild)[2023-05-12]
您可以使用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 ... -
有效地查询关系(Query relationships efficiently)[2023-04-06]
有可能为您的查询做到这一点,但由于最终目标是生成一个分层对象模型,所以您花费的时间和精力将花费在研究可自动执行大部分繁重工作的对象/关系映射器上。 有什么可用取决于客户端应用程序的编程语言; 维基百科列出了一些列表。 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 ...
-
如何根据子项密钥的值有效地进行查询?(How to efficiently make a query based on the value of a subchild key?)[2022-12-17]
如果要有一个显示每个用户的最新时间戳的列表,则应存储一个列表,该列表显示数据库中每个用户的最新时间戳。 LatestTimstampForUserID: - [UserID01]: [TimeStamp02] - [UserID02]: [TimeStamp04] ... 现在,您可以非常有效地从数据库中读取此数据并显示列表。 成本是,当为用户发布新消息时,您需要更新数据库中的多个位置。 这在使用NoSQL数据库(例如Firebase实时数据库)时非常常见。 If you want to ...