You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法将指定SQL查询转换为LINQ查询,寻求技术解决方案

SQL转LINQ查询解决方案

查询语法(Query Syntax)

var result = from sy in context.Survey.AsNoTracking()
             join ap in context.AssetPurity.AsNoTracking()
             on new { sy.LocationId, sy.SessionUID } equals new { ap.LocationId, ap.SessionUID }
             where ap.SceneTypeCode != 5 && ap.SceneSubTypeCode != 1
                && sy.LocationId == 1
                && sy.SurveyStatusId == 1
                && sy.IsDeleted == 0
                && sy.SurveyDate >= curPeriod.startPeriodDate.Value
                && sy.SurveyDate <= curPeriod.endPeriodDate.Value
             select sy;

方法语法(Method Syntax)

var result = context.Survey.AsNoTracking()
    .Join(
        context.AssetPurity.AsNoTracking(),
        sy => new { sy.LocationId, sy.SessionUID },
        ap => new { ap.LocationId, ap.SessionUID },
        (sy, ap) => new { sy, ap }
    )
    .Where(x => x.ap.SceneTypeCode != 5 && x.ap.SceneSubTypeCode != 1
                && x.sy.LocationId == 1
                && x.sy.SurveyStatusId == 1
                && x.sy.IsDeleted == 0
                && x.sy.SurveyDate >= curPeriod.startPeriodDate.Value
                && x.sy.SurveyDate <= curPeriod.endPeriodDate.Value)
    .Select(x => x.sy);

关键说明

  • AsNoTracking()对应SQL中的WITH(NOLOCK),关闭EF实体追踪以提升查询性能,若需要严格的读未提交隔离级别,需额外设置context.Database.SetTransactionIsolationLevel(IsolationLevel.ReadUncommitted)
  • 多字段关联时用匿名类型匹配,确保关联字段的名称、类型完全一致
  • 最终选择sy对象,对应SQL中SELECT SY.*的需求

内容的提问来源于stack exchange,提问作者Shailendra Pal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 02:57:12