在另一个查询的where子句中嵌套LINQ查询

huangapple go评论80阅读模式
英文:

Nesting a LINQ query inside a where clause of another query

问题

我如何将下面两个查询合并为单个查询?

使用(var ctx = ContextFactory.CreateDbContext())
{
int parentId = await ctx.MenuEntity.Where(m => m.Title == thisVariable).Select(m => m.MenuId).SingleAsync();
menuEntityList = await ctx.MenuEntity.Where(m => m.ParentId == parentId).ToListAsync();
}

英文:

How would I combine the following two queries into a single query?

    using (var ctx = ContextFactory.CreateDbContext())
    {
        int parentId = await ctx.MenuEntity.Where(m => m.Title == thisVariable).Select(m => m.MenuId).SingleAsync();
        menuEntityList = await ctx.MenuEntity.Where(m => m.ParentId == parentId).ToListAsync();
    }

答案1

得分: 1

你可以使用 LINQ 中的 Join 来实现。

代码示例:

using (var ctx = ContextFactory.CreateDbContext())
{
   menuEntityList = await (
        from m in ctx.MenuEntity
        where m.ParentId == ctx.MenuEntity
            .Where(x => x.Title == thisVariable)
            .Select(x => x.MenuId)
            .Single()
        select m
   ).ToListAsync();
}

然而,正如你所看到的,这段代码并没有提供很大的优势,我猜在几乎每个方面两种选项可能都差不多。

英文:

Combine two LINQ queries in C#

You can do it by using Join in LINQ.

Code Example:

using (var ctx = ContextFactory.CreateDbContext())
{
   menuEntityList = await (
        from m in ctx.MenuEntity
        where m.ParentId == ctx.MenuEntity
            .Where(x => x.Title == thisVariable)
            .Select(x => x.MenuId)
            .Single()
        select m
   ).ToListAsync();
}

However as you can see the code does not provide a huge advantage and I guess both options probably same in almost every perspective.

答案2

得分: 1

使用稍微不同的语法,更像是 SQL 中的 `IN` 语句:
```cs
using (var ctx = ContextFactory.CreateDbContext())
{
   menuEntityList = await (
        from m in ctx.MenuEntity
        where ctx.MenuEntity
            .Where(x => x.Title == thisVariable)
            .Select(x => x.MenuId)
            .Contains(m.ParentId)
        select m
   ).ToListAsync();
}
英文:

A slightly different syntax from the answer by @AztecCodes, more like an IN in SQL

using (var ctx = ContextFactory.CreateDbContext())
{
   menuEntityList = await (
        from m in ctx.MenuEntity
        where ctx.MenuEntity
            .Where(x => x.Title == thisVariable)
            .Select(x => x.MenuId)
            .Contains(m.ParentId)
        select m
   ).ToListAsync();
}

答案3

得分: 1

这个查询等同于SQL中的EXISTS。

var menuEntityList = context.MenuEntity.Where(
    m => context.MenuEntity.Where(m =>
        m.Title == thisVariable).Any(d => d.MenuId == m.ParentId)
).ToList();

我使用Profiler获得了查询结果。

exec sp_executesql N'SELECT [m].[Id], [m].[MenuId], [m].[ParentId], [m].[Title]
FROM [MenuEntity] AS [m]
WHERE EXISTS (
    SELECT 1
    FROM [MenuEntity] AS [m0]
    WHERE [m0].[Title] = @__thisVariable_0 
    AND [m0].[MenuId] = [m].[ParentId])',
N'@__thisVariable_0 nvarchar(4000)',@__thisVariable_0=N'Org'

注意:上面的内容已经被我翻译了,不包含其他信息。

英文:

This query is equivalent to EXISTS in sql

 var menuEntityList = context.MenuEntity.Where(
 m => context.MenuEntity.Where(m =>
 m.Title == thisVariable).Any(d => d.MenuId == m.ParentId)
                ).ToList();


I get Query with Profiler

exec sp_executesql N'SELECT [m].[Id], [m].[MenuId], [m].[ParentId], [m].[Title]
FROM [MenuEntity] AS [m]
WHERE EXISTS (
    SELECT 1
    FROM [MenuEntity] AS [m0]
    WHERE [m0].[Title] = @__thisVariable_0 
	AND [m0].[MenuId] = [m].[ParentId])'
,N'@__thisVariable_0 nvarchar(4000)',@__thisVariable_0=N'Org'

huangapple
  • 本文由 发表于 2023年6月1日 03:43:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定