linq查询的问题
我有一个自引用表“产品”,其结构如下(其中 D = 草稿且 A = 已批准)
ID ParentID Status Name
---------------------------
1 NULL A Foo
2 1 A Foo2
3 NULL D Bar
4 1 D Foo3
行可以是“新”行(其中 ParentID == null),也可以是现有行的版本。所以我们从表中可以看到,“Foo”项有 3 个版本,而“Bar”只有 1 个版本。
我需要一种根据用户是否只能看到“已批准”项目或也可以看到“草稿”来返回每个项目的最新版本的方法。例如,
可以看到“D”的用户将拥有:
3 NULL D
4 1 D
“Foo”和“Bar”的“最新”行。
可以看到“A”的用户会:
2 1 A
即。仅“已批准”版本。
预先感谢,
何塞
I have a self referencing table "Product" with the following structure (where D = Draft and A = Approved)
ID ParentID Status Name
---------------------------
1 NULL A Foo
2 1 A Foo2
3 NULL D Bar
4 1 D Foo3
A row can either be "new" (where ParentID == null) or can be a version of an existing row. So we can see from the table that there are 3 versions for the item "Foo" and only 1 for "Bar".
I need a way of returning the latest versions of each item based on whether the user is able to see only "Approved" items or is able to see "Draft" as well. So for example
Users who can see "D" would have:
3 NULL D
4 1 D
The "latest" row for "Foo" and "Bar".
Users who can see "A" would have:
2 1 A
ie. only the "Approved" versions.
Thanks in advance,
Jose
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是应该适合您的 Linq 查询:
这是正在发生的事情:
我应该注意,如果您有能力更改数据模式,您应该考虑添加一个名为 InsertDate 的列或其他列那个效果。现在我假设 ID 最高的记录是最新的。通常最好添加一个日期时间字段并对其进行排序。
我很抱歉这实际上并没有使用 Linq 语法——我更喜欢流畅的编码风格——但如果您愿意的话,它可以很容易地转换为 Linq 语法。
Here is the Linq query that should work for you:
Here's what's happening:
I should note that if you have the ability to change your data schema you should consider adding a column called InsertDate or something to that effect. Right now I am assuming that whatever record has the highest ID is the latest. It is often better to add a DateTime field and sort on that instead.
I apologize that this isn't actually using Linq syntax--I prefer fluent coding styles--but it could be easily translated to Linq syntax if you preferred it.
完全未经测试 - 但如果我正确理解了这个问题,这样的东西可能会起作用。
可以看到已批准
可以看到已批准和草稿
Totally untested - but something like this might work, if I've understood the question correctly.
Can see approved
Can see approved and draft