C# 尝试在LINQ中实现SQL查询
我试图在LINQ中实现一个SQL查询,但我找不到哪里出了问题 原始SQL命令:C# 尝试在LINQ中实现SQL查询,c#,sql,linq,C#,Sql,Linq,我试图在LINQ中实现一个SQL查询,但我找不到哪里出了问题 原始SQL命令: SELECT DISTINCT a.art_id, a.art_title,a.art_permalink, MAX(sp.sp_code), MAX(p.pr_cat_name), MAX(p.pr_value), MAX(p.pr_cat_id), MAX(p.pr_cat_id) FROM vArtPart sp JOIN vArticle a ON (sp.art_id = a.art_id) JOIN v
SELECT DISTINCT a.art_id, a.art_title,a.art_permalink, MAX(sp.sp_code), MAX(p.pr_cat_name), MAX(p.pr_value), MAX(p.pr_cat_id), MAX(p.pr_cat_id)
FROM vArtPart sp
JOIN vArticle a ON (sp.art_id = a.art_id)
JOIN vPrice p ON (a.art_id = p.art_id)
WHERE
sp.sp_id = 31 AND
a.art_visible = 1 AND
a.lng_id = 1
GROUP BY a.art_id, a.art_title, a.art_permalink
var q = (from sp in db.vArtParts
where sp.sp_id == dd_type.SelectedValue.ToInt()
join a in db.vArticles on sp.art_id equals a.art_id into aa
from a in aa.DefaultIfEmpty()
join p in db.vPrices on a.art_id equals p.art_id into pp
from p in pp.DefaultIfEmpty()
where a.art_visible == true && a.lng_id == 1
select new
{
art_id = (int?)a.art_id,
a.art_title,
a.art_permalink,
sp.sp_code,
p.pr_cat_name,
p.pr_value,
pr_cat_id = (int?)p.pr_cat_id
}).ToList();
Linq查询尝试(不工作)
*upd:同一个LINQ查询在没有分组的情况下工作(但我需要它):
SELECT DISTINCT a.art_id, a.art_title,a.art_permalink, MAX(sp.sp_code), MAX(p.pr_cat_name), MAX(p.pr_value), MAX(p.pr_cat_id), MAX(p.pr_cat_id)
FROM vArtPart sp
JOIN vArticle a ON (sp.art_id = a.art_id)
JOIN vPrice p ON (a.art_id = p.art_id)
WHERE
sp.sp_id = 31 AND
a.art_visible = 1 AND
a.lng_id = 1
GROUP BY a.art_id, a.art_title, a.art_permalink
var q = (from sp in db.vArtParts
where sp.sp_id == dd_type.SelectedValue.ToInt()
join a in db.vArticles on sp.art_id equals a.art_id into aa
from a in aa.DefaultIfEmpty()
join p in db.vPrices on a.art_id equals p.art_id into pp
from p in pp.DefaultIfEmpty()
where a.art_visible == true && a.lng_id == 1
select new
{
art_id = (int?)a.art_id,
a.art_title,
a.art_permalink,
sp.sp_code,
p.pr_cat_name,
p.pr_value,
pr_cat_id = (int?)p.pr_cat_id
}).ToList();
*upd:错误消息:
SELECT DISTINCT a.art_id, a.art_title,a.art_permalink, MAX(sp.sp_code), MAX(p.pr_cat_name), MAX(p.pr_value), MAX(p.pr_cat_id), MAX(p.pr_cat_id)
FROM vArtPart sp
JOIN vArticle a ON (sp.art_id = a.art_id)
JOIN vPrice p ON (a.art_id = p.art_id)
WHERE
sp.sp_id = 31 AND
a.art_visible = 1 AND
a.lng_id = 1
GROUP BY a.art_id, a.art_title, a.art_permalink
var q = (from sp in db.vArtParts
where sp.sp_id == dd_type.SelectedValue.ToInt()
join a in db.vArticles on sp.art_id equals a.art_id into aa
from a in aa.DefaultIfEmpty()
join p in db.vPrices on a.art_id equals p.art_id into pp
from p in pp.DefaultIfEmpty()
where a.art_visible == true && a.lng_id == 1
select new
{
art_id = (int?)a.art_id,
a.art_title,
a.art_permalink,
sp.sp_code,
p.pr_cat_name,
p.pr_value,
pr_cat_id = (int?)p.pr_cat_id
}).ToList();
错误CS1061:“System.Linq.IGrouping”不存在
不包含“艺术id”的定义,也不包含扩展方法“艺术id”
接受类型为的第一个参数
可以找到“System.Linq.IGrouping”(是
是否缺少using指令或程序集引用?)
ASP.global\u asax.Application\u Error Error System.Web.HttpParseException(0x80004005):***\ctrl\u search\u product.ascx.cs(70):错误CS1061:'System.Linq.IGrouping'不包含'art\u id'的定义,并且找不到接受'System.Linq.IGrouping'类型的第一个参数的扩展方法'art\u id'(是否缺少using指令或程序集引用?->System.Web.httpcompileeException(0x80004005):c:\inetpub\vhosts\domostroy msk.ru\httpdocs\inc\ctrl\u search\u product.ascx.cs(70):错误CS1061:“System.Linq.IGrouping”不包含“art\u id”的定义,并且找不到接受“System.Linq.IGrouping”类型的第一个参数的扩展方法“art\u id”(是否缺少using指令或程序集引用?)
位于System.Web.Compilation.BuildManager.PostProcessFoundBuildResult(BuildResult结果,布尔键FromVPP,VirtualPath VirtualPath)
位于System.Web.Compilation.BuildManager.GetBuildResultFromCacheInternal(String cacheKey、Boolean keyFromVPP、VirtualPath VirtualPath、Int64 hashCode、Boolean EnsueUpdate)
位于System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath VirtualPath,布尔值,最新)
位于System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath、Boolean noBuild、Boolean allowCrossApp、Boolean allowBuildInPrecompile、Boolean throwIfNotFound、Boolean EnsureUpdate)
在System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext上下文、VirtualPath VirtualPath、Boolean noBuild、Boolean allowCrossApp、Boolean allowBuildInPrecompile、Boolean throwIfNotFound、Boolean EnsureUpdate)
位于System.Web.UI.BaseTemplateParser.GetReferencedType(VirtualPath VirtualPath,布尔allowNoCompile)
位于System.Web.UI.BaseTemplateParser.GetUserControlType(VirtualPath VirtualPath)
位于System.Web.UI.MainTagNameToTypeMapper.ProcessUserControlRegistration(UserControlRegisterEntry-ucRegisterEntry)
位于System.Web.UI.BaseTemplateParser.ProcessDirective(String directiveName,IDictionary指令)
位于System.Web.UI.TemplateControlParser.ProcessDirective(String directiveName,IDictionary指令)
位于System.Web.UI.PageParser.ProcessDirective(String directiveName,IDictionary指令)
位于System.Web.UI.TemplateParser.ParseStringInternal(字符串文本,编码文件编码)
位于System.Web.UI.TemplateParser.ProcessException(异常ex)
位于System.Web.UI.TemplateParser.ParseStringInternal(字符串文本,编码文件编码)
位于System.Web.UI.TemplateParser.ParseString(字符串文本,VirtualPath,编码文件编码)
位于System.Web.UI.TemplateParser.ParseFile(字符串physicalPath,VirtualPath)
在System.Web.UI.TemplateParser.ParseInternal()中
在System.Web.UI.TemplateParser.Parse()中
在System.Web.Compilation.BaseTemplateBuildProvider.get_CodeCompilerType()中
位于System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider BuildProvider)
位于System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders()处
在System.Web.Compilation.BuildProvidersCompiler.PerformBuild()中
位于System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath VirtualPath)
位于System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath、Boolean noBuild、Boolean allowCrossApp、Boolean allowBuildInPrecompile、Boolean throwIfNotFound、Boolean EnsureUpdate)
在System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext上下文、VirtualPath VirtualPath、Boolean noBuild、Boolean allowCrossApp、Boolean allowBuildInPrecompile、Boolean throwIfNotFound、Boolean EnsureUpdate)
位于System.Web.Compilation.BuildManager.GetVirtualPath对象工厂(VirtualPath VirtualPath、HttpContext上下文、布尔allowCrossApp、布尔throwIfNotFound)
位于System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath VirtualPath,类型requiredBaseType,HttpContext上下文,布尔allowCrossApp)
位于System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext上下文、字符串请求类型、VirtualPath VirtualPath、字符串物理路径)
位于System.Web.UI.PageHandlerFactory.GetHandler(HttpContext上下文、字符串requestType、字符串virtualPath、字符串路径)
在System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()中
在System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)
由于您正在分组,因此在最终选择中,您需要
select new
{
art_id = (int?)g.key.art_id,
g.key.art_title,
g.key.art_permalink,
...
}
另外,与您看到的错误无关,在您发布的SQL版本中,您正在执行内部联接,但在LINQ版本中,您正在执行左外部联接;您应该删除DefaultIfEmpty行
编辑:由于您现在在最终选择部分遇到问题,下面是后一个错误的答案(请注意,'group sp by…'变成group new{sp,p}by…'-
var q = (from sp in db.vArtParts
where sp.sp_id == dd_type.SelectedValue.ToInt()
join a in db.vArticles on sp.art_id equals a.art_id
join p in db.vPrices on a.art_id equals p.art_id
where a.art_visible == true && a.lng_id == 1
group new {sp, p} by new {a.art_id, a.art_title, a.art_permalink} into g
select new
{
art_id = (int?)g.Key.art_id,
g.Key.art_title,
g.Key.art_permalink,
sp_code = g.Max(gg=>gg.sp.sp_code),
pr_cat_name = g.Max(gg=>gg.p.pr_cat_name),
pr_value = g.Max(gg=>gg.p.pr_value),
pr_cat_id = (int?)g.Max(gg=>gg.p.pr_cat_id)
}).ToList();
编译时或运行时错误消息或其他结果?如果可用,您可以尝试在IQueryEnable上调用ToTraceString以检查LINQ查询将生成的SQL。错误消息不可用。但是查询在不分组的情况下工作,例如(我更新了问题内容)
sp.sp_id=31
是否不必要?为什么不将其包含在LINQ中?发布错误消息