Linq to SQL 查询返回数据

Linq to SQL Query Returning Data

我的数据服务中有这个方法,它返回两个字段 DisplayName 和 Version Key。当我查看正在访问服务器的查询时,它是两个 select 语句?有没有更好的方法来编写此 Linq,使其仅 returns 我需要的数据?我没有使用 toList(),所以我不确定为什么它会返回这么多数据。

型号Class

 public class AgentDto
    {
        public long Id { get; set; }

        public string Name { get; set; }

        public Guid? VersionKey { get; set; }
    }

数据服务

 public IQueryable<Data.Dto.Agent.AgentDto> GetPublishedAgent()
        {
            var Agent = (from t in UnitOfWork.GetRepository<Template>().Get()
                                join r in UnitOfWork.GetRepository<Regimen>().Get() on t.Id equals r.TemplateId
                                join rp in UnitOfWork.GetRepository<RegimenPart>().Get() on r.Id equals rp.RegimenId
                                join re in UnitOfWork.GetRepository<RegimenEntry>().Get() on rp.Id equals re.RegimenPartId
                                join a in UnitOfWork.GetRepository<Agent>().Get() on re.AgentVersionKey equals a.VersionKey
                                where t.IsCurrentVersion && t.Status == 7 && a.IsCurrentVersion && a.IsActive
                                select new Data.Dto.Agent.AgentDto
                                {
                                 Name = a.DisplayName,
                                 VersionKey= a.VersionKey
                                 });
            Agent = Agent.Distinct();
            return Agent;
        }

服务器分析器

SELECT DISTINCT [t5].[Id], [t5].[Name], [t5].[Added], [t5].[Modified], [t5].[Deleted], [t5].[IsDeleted], [t5].[RxNormId], [t5].[BrandNames], [t5].[IsFreeText], [t5].[AddedBy], [t5].[ModifiedBy], [t5].[DeletedBy], [t5].[RxNormText], [t5].[NccnTallMan], [t5].[RxNormTallMan], [t5].[IsActive], [t5].[VersionKey], [t5].[VersionNumber], [t5].[IsCurrentVersion], [t5].[value] AS [VersionKey2]
FROM (
    SELECT [t4].[Id], [t4].[Name], [t4].[Added], [t4].[Modified], [t4].[Deleted], [t4].[IsDeleted], [t4].[RxNormId], [t4].[BrandNames], [t4].[IsFreeText], [t4].[AddedBy], [t4].[ModifiedBy], [t4].[DeletedBy], [t4].[RxNormText], [t4].[NccnTallMan], [t4].[RxNormTallMan], [t4].[IsActive], [t4].[VersionKey], [t4].[VersionNumber], [t4].[IsCurrentVersion], [t4].[VersionKey] AS [value], [t0].[IsCurrentVersion] AS [IsCurrentVersion2], [t0].[Status]
    FROM [dbo].[Templates] AS [t0]
    INNER JOIN [dbo].[Regimens] AS [t1] ON [t0].[Id] = [t1].[TemplateId]
    INNER JOIN [dbo].[RegimenParts] AS [t2] ON [t1].[Id] = [t2].[RegimenId]
    INNER JOIN [dbo].[RegimenEntries] AS [t3] ON [t2].[Id] = [t3].[RegimenPartId]
    INNER JOIN [dbo].[Agents] AS [t4] ON [t3].[AgentVersionKey] = [t4].[VersionKey]
    ) AS [t5]
WHERE ([t5].[IsCurrentVersion2] = 1) AND ([t5].[Status] = @p0) AND ([t5].[IsCurrentVersion] = 1) AND ([t5].[IsActive] = 1)',N'@p0 int',@p0=7

显示名称方法

  public string DisplayName
        {
            get
            {                
                if (!string.IsNullOrEmpty(this.RxNormTallMan))
                {
                    return this.RxNormTallMan;
                }
                else if (!string.IsNullOrEmpty(this.NccnTallMan))
                {
                    return this.NccnTallMan;
                }

                return this.Name;
            }
        }

这里的关键是 DisplayName 不是 映射 属性。 IE。 is 与数据库字段不对应。因此,属性 没有 SQL 翻译。 LINQ-to-SQL 检测到并决定它能做的最好的事情是获取所有表的所有字段并构建所需的投影 (AgentDto) 客户端,即内存中。

如果您希望 L2S select 仅需要的字段,您必须在 select 语句中仅使用映射属性:

select new Data.Dto.Agent.AgentDto
{
    Name = a.RxNormTallMan.Length > 0 
        ? a.RxNormTallMan 
        : a.NccnTallMan.Length > 0
            ? a.NccnTallMan
            : a.Name,
    VersionKey= a.VersionKey 
});