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

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

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?

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

答案1

得分: 1

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

代码示例:

  1. using (var ctx = ContextFactory.CreateDbContext())
  2. {
  3. menuEntityList = await (
  4. from m in ctx.MenuEntity
  5. where m.ParentId == ctx.MenuEntity
  6. .Where(x => x.Title == thisVariable)
  7. .Select(x => x.MenuId)
  8. .Single()
  9. select m
  10. ).ToListAsync();
  11. }

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

英文:

Combine two LINQ queries in C#

You can do it by using Join in LINQ.

Code Example:

  1. using (var ctx = ContextFactory.CreateDbContext())
  2. {
  3. menuEntityList = await (
  4. from m in ctx.MenuEntity
  5. where m.ParentId == ctx.MenuEntity
  6. .Where(x => x.Title == thisVariable)
  7. .Select(x => x.MenuId)
  8. .Single()
  9. select m
  10. ).ToListAsync();
  11. }

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

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

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

  1. using (var ctx = ContextFactory.CreateDbContext())
  2. {
  3. menuEntityList = await (
  4. from m in ctx.MenuEntity
  5. where ctx.MenuEntity
  6. .Where(x => x.Title == thisVariable)
  7. .Select(x => x.MenuId)
  8. .Contains(m.ParentId)
  9. select m
  10. ).ToListAsync();
  11. }

答案3

得分: 1

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

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

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

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

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

英文:

This query is equivalent to EXISTS in sql

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

I get Query with Profiler

  1. exec sp_executesql N'SELECT [m].[Id], [m].[MenuId], [m].[ParentId], [m].[Title]
  2. FROM [MenuEntity] AS [m]
  3. WHERE EXISTS (
  4. SELECT 1
  5. FROM [MenuEntity] AS [m0]
  6. WHERE [m0].[Title] = @__thisVariable_0
  7. AND [m0].[MenuId] = [m].[ParentId])'
  8. ,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:

确定