[英]How do I do queries against hierarchical data using LINQ to SQL?
I have 2 tables that are related.我有 2 个相关的表。 Each app can relate to many apps.
每个应用程序都可以与许多应用程序相关联。 ie, office can be related to word, excel.......
即office可以关联word,excel.......
app应用程序
id PK int id PK 整数
appname varchar(50)应用程序名 varchar(50)
..... ......
appsrelated应用相关
relatedid int fk to app.id relatedid int fk 到 app.id
appid int appid int
sample data app id, appname示例数据应用程序 ID、应用程序名称
1, office 1、办公室
2, word 2、字
3, excel 3、excel
4, quake 4、地震
appsrelated relatedid, appid应用相关的相关id、appid
1, 2 1, 2
1, 3 1、3
Basically, I'm new to linq-to-sql an I have Brain lock.基本上,我是 linq-to-sql 的新手,我有脑锁。
I would like to do the following query.我想做以下查询。 I use vb.net but c# is ok.
我使用 vb.net 但 c# 没问题。 Query is to return all the apps that are not related to (1), so the result should be (4, quake).
查询是返回所有与(1)无关的app,所以结果应该是(4,quake)。
Thanks in advance.提前致谢。
The following code should accomplish what you are asking if I understood correctly.如果我理解正确,以下代码应该可以完成您的要求。
var relatedToApp1 = Context.appsrelated.Where(related => related.relatedid == 1);
var items = Context.app.Where(app => app.id != 1 && !relatedToApp1.Any(related => related.appid == app.id));
C# -- find the ids of the related apps, select ids, select only those apps that aren't the app in question or whose ids don't appear in the ids of the apps that are related. C# - 查找相关应用程序的 id,select ids,select 仅那些与相关应用程序无关的应用程序或其 id 不相关的应用程序。
var query = apps.Where( a => a.appid != 1
&& !appsrelated.Where( r => r.relatedid == 1 )
.Select( r => r.appid )
.Contains( a.appid ) );
for those in vb land.对于那些在vb土地上的人。 here is the resulting query.
这是结果查询。
Dim dc As New dashboardDataContext
Dim q = dc.appsrelateds _
.Where(Function(r) r.relatedid = 17)
Dim items = dc.ApplicationInfos _
Where(Function(app) app.Id <> 17 And Not q.Any(Function(related) related.appid = app.Id))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.