简体   繁体   English

如何使用 LINQ 到 SQL 对分层数据进行查询?

[英]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.

相关问题 如何对具有不同架构的数据库执行 LINQ 查询? - How do I execute LINQ queries against a database with varying schema? 使用LINQ,如何将分层XML加载到POCO中? - Using LINQ, how do I load a hierarchical XML into a POCO? 我如何使用linq对集合对象执行WHERE操作(使用Netflix数据源) - How do I use linq to do a WHERE against a collection object (using Netflix data source) 针对实体框架的LINQ查询如何将日期传递给SQL Server? - How do LINQ queries against the Entity Framework communicate dates to a SQL Server? 如何提高此通用Linq-to-SQL数据访问类执行的查询的效率? - How do I improve the efficiency of the queries executed by this generic Linq-to-SQL data access class? 如何在LINQ to EF中针对空集合的查询合并.Max()? - How do I coalesce .Max() for queries against empty collections in LINQ to EF? 如何在运行时使用字符串构建动态linq查询? - how do i build dynamic linq queries at runtime using strings? 如何将2个Linq查询合并为1个? - How do i combine 2 Linq queries into 1? 参数化查询如何帮助抵御 SQL 注入? - How do parameterized queries help against SQL injection? 如何使用 LINQ 对 Azure Cosmos Document DB SQL ZDB9474238D04ADE1 有效地进行动态查询 - How to effectively do dynamic query using LINQ against a Azure Cosmos Document DB SQL API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM