gpt4 book ai didi

linq - 在 LINQ 中仅选择一列

转载 作者:行者123 更新时间:2023-12-03 11:16:21 25 4
gpt4 key购买 nike

EntityModel 定义为:
人员有一个国家的链接

在 LinqPad 中执行此代码时,我看到在第一个查询中生成的 SQL 未优化(返回所有字段)?我在这里错过了什么或做错了什么?

查询 1 LINQ

var Country = Countries.FirstOrDefault(o => o.Id == 100000581);
var personnelIds = Country.Personnels.Select(p => p.Id).ToArray();
personnelIds.Dump();

查询 1 SQL
exec sp_executesql N'SELECT [t0].[Id], [t0].[Version], [t0].[Identifier], [t0].[Name], , [t0].[UpdatedBy] FROM [Personnel] AS [t0] WHERE [t0].[Country_Id] = @p0',N'@p0 bigint',@p0=100000581


查询 2 LINQ
var Country = Countries.FirstOrDefault(o => o.Id == 100000581);
var personnelIds2 = Personnels.Where(p => p.Country == Country).Select(p => p.Id).ToArray();
personnelIds2.Dump();

查询 2 SQL
exec sp_executesql N'SELECT [t0].[Id] FROM [Personnel] AS [t0] WHERE [t0].[Country_Id] = @p0',N'@p0 bigint',@p0=100000581

使用的数据库是 SQL Express 2008。LinqPad 版本是 4.43.06

最佳答案

//var Country = Countries.FirstOrDefault(o => o.Id == 100000581);
var personnelIds = context.Personnels
.Where(p => p.Country.Id == 100000581)
.Select(p => p.Id)
.ToArray();

personnelIds.Dump();

试试这个,应该会更好。

关于linq - 在 LINQ 中仅选择一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15690756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com