Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将SQL server查询转换为Linq_C#_Linq_Sql Server 2008 - Fatal编程技术网

C# 将SQL server查询转换为Linq

C# 将SQL server查询转换为Linq,c#,linq,sql-server-2008,C#,Linq,Sql Server 2008,下面提到的查询位于SQL Server 2008中: SELECT * FROM Posts p INNER JOIN Categories c ON p.CategoryId = c.CategoryId INNER JOIN Users u ON p.UserId = u.UserId INNER JOIN Tags t ON p.TagId = t.TagId INNER JOIN Locations l ON p.LocationId = l.LocationId left JOI

下面提到的查询位于SQL Server 2008中:

SELECT * FROM Posts p 
INNER JOIN Categories c ON p.CategoryId = c.CategoryId
INNER JOIN Users u ON p.UserId = u.UserId
INNER JOIN Tags t ON p.TagId = t.TagId 
INNER JOIN Locations l ON p.LocationId = l.LocationId 
left JOIN PostImages pm ON p.PostId = pm.PostId
WHERE p.CategoryId = 1 and **pm.PostimageId = (select top 1 postimageid from PostImages where PostImages.PostId=p.postid)**
现在我想将上面的SQL查询转换为LINQ

我的LINQ查询:

   var objPosts = (from p in _dbcontext.Posts
                            join us in _dbcontext.Users on p.UserId equals us.UserId
                            join tag in _dbcontext.Tags on p.TagId equals tag.TagId
                            join cat in _dbcontext.Categories on p.CategoryId equals cat.CategoryId
                            join loc in _dbcontext.Locations on p.LocationId equals loc.LocationId
                            join img in _dbcontext.PostImages on p.PostId equals img.PostId into gj
                            from postimg in gj.DefaultIfEmpty()

                            where p.Disabled == false && p.CategoryId == userPost.CategoryId || p.UserId == userPost.UserId || p.TagId == userPost.TagId || p.LocationId == userPost.LocationId 
                            && postimg.PostImageId == (from pm in _dbcontext.PostImages where pm.PostImageId == p.PostId)


                            orderby p.PostId descending

                            select new
                            {
                                PostId = p.PostId,
                                PostTitle = p.Title,
                                //ImageInfo = postimg.ImagePath,
                                //ThumbNailInfo = p.ThubNailInfo,
                                PostShortDescription = p.ShortDescription,
                                UserId = us.UserId,
                                UserName = us.Name,
                                TagId = tag.TagId,
                                TagTitle = tag.TagTitle,
                                CategoryId = cat.CategoryId,
                                CategoryName = cat.CategoryName,
                                LocationId = loc.LocationId,
                                LocationName = loc.LocationName
                            });
我几乎完成了,但是我无法将上面提到的SQL查询转换为LINQ


谢谢

似乎您缺少了内部查询的
First()
调用(对应于SQL查询的
top 1
部分):


请按如下方式使用FirstOrDefault:

&& postimg.PostImageId == (from pm in _dbcontext.PostImages where pm.PostImageId == p.PostId).FirstOrDefault()

我知道这不是相关的答案,但它肯定会起作用。你听说了吗。如果没有,请试一试。

它给了我一个错误:查询体必须以select子句或group by子句结尾
&& postimg.PostImageId == (from pm in _dbcontext.PostImages where pm.PostImageId == p.PostId).FirstOrDefault()