简体   繁体   English

搜索SQL表(LINQ或SqlCommand)

[英]Searching through SQL Table (LINQ or SqlCommand)

Im trying to search a item table, there is an id, name and oldId (can be null) 我试图搜索一个项目表,有一个ID,名称和oldId(可以为空)

When an item is updated a new row is created with the new data, and the old id of the item is set to the new rows OldId field. 更新项目时,将使用新数据创建新行,并将该项目的旧ID设置为新行OldId字段。

But i cant figgure out how to show the latest revision of the item and those who are unedited during search. 但是我无法弄清楚如何显示该项目的最新版本以及那些在搜索过程中未编辑的项目。

var q = from i in Items where i.Name.Contains(x) select i;

Ideas? 有想法吗?

If I understand your question correctly, each row whose ID is not used in any other row in the OldId column is the latest revision of its item. 如果我正确理解了您的问题,则OldId列的任何其他行中未使用其ID的每一行都是该项目的最新版本。

This would lead to the following: 这将导致以下情况:

var latestRevisions = Items.Where(x => !Items.Any(y => y.OldId == x.Id));

In SQL, I would write it like this: 在SQL中,我会这样写:

SELECT
    i.* 
FROM
    items i
    LEFT OUTER JOIN items i2
        ON  i.item_id = i2.old_item_id
WHERE
    i2.item_id IS NULL
;

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

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