实体框架查询和性能

时间:2017-06-19 05:34:53

标签: c# model-view-controller

我有一个类似社交网络的项目 我必须写一个查询来获取一些帖子和帖子的最后评论我的粉丝(未被阻止)喜欢那个 我的代码

['Application', 'Application.UserAuthRequest', 'Application.UserAuthRequest.VendorApp', 'Application.UserAuthRequest.VendorApp.AppName', 'Application.ApplicationRequest', 'Application.ApplicationRequest.GUID', 'Application.ApplicationRequest.Type', 'Application.ApplicationRequest.File', 'Application.ApplicationRequest.FileExtension', 'Application.ApplicationRequest.FileExtension.Result', 'Application.ApplicationRequest.FileExtension.Result.ResultCode']
['', '', '', 'SING', '', 'ABD45129-PD1212-121DFL', 'Streaming', '', '', '', 'Success']
[{'Version': '2.01'}, {}, {}, {}, {'ID': '12-123-AH'}, {}, {'tc': '200'}, {}, {'VendorCode': '200'}, {}, {'tc': '1'}]

此代码生成sqlQuery,16个select和50ms运行!!!!

我的错误在哪里 感谢

sql Query:

public List<PostProject> GetFavoritePosts(string userId, int currentPage, int noOfRecords)
    {
        var skipPosts = noOfRecords * currentPage;
        int i = -260;
        var day = DateTime.Now.AddDays(i);

        var blockedusers = DataContext.BlockedUsers.Where(bu => bu.BlockerId == userId);
        var followers = DataContext.FollowUser.Where(u => u.FollowFromUserId == userId);
        var posts = DataContext.Posts.Where(p => p.UserId != userId
        && p.DateOfUpdate > day
        && p.Likes.Any(l => followers.Any(fu => fu.FollowToUserId == l.UserId))
        && p.PostStatusId == 1
        && p.PostType == false
        && blockedusers.All(bu => bu.BlockedId != p.UserId));

        var feed = posts

            .OrderByDescending(post => post.Likes.Count)
            .Select(post => new PostProject
            {
                PostId = post.PostId,
                Content = post.Content,
                Image = post.Image,
                Location = post.Location,
                Video = post.Video,
                CreatedDate = post.CreatedDate,
                DateOfUpdate = post.DateOfUpdate,
                User = post.User,
                ILiked = post.Likes.Any(like => like.UserId == userId),
                LikeCount = post.Likes.Count,
                CommentsCount = post.Comments.Count,
                PostStatusId = post.PostStatusId,
                ShareCount = post.SharePosts.Count,
                Comments = new List<Comment> { post.Comments.OrderByDescending(c =>   c.CommentDate).FirstOrDefault() }
            });
        return feed.Skip(() => skipPosts).Take(() => noOfRecords).ToList();
    }

2 个答案:

答案 0 :(得分:1)

首先,似乎Linq和Linq之间存在混淆。并非所有Linq都可以转换为SQL查询。例如:Any()All()不能与Linq to Sql一起使用 - 它们是内存中的集合函数。这意味着需要获取所有行,然后再解析。

您也没有解决前两个查询,例如通过调用ToList()

var blockedusers = DataContext.BlockedUsers.Where(bu => bu.BlockerId == userId);
var followers = DataContext.FollowUser.Where(u => u.FollowFromUserId == userId);

这使得它们在每次使用时都按需运行(延迟查询执行) - 这意味着它们每次都调用SQL。当心IEnumerable!

您可以通过在同一查询中对BlockedUser和FollowUser进行连接来摆脱所有问题。例如,使用左连接到BlockedUser并通过测试被阻止的用户为空来消除这些行。

答案 1 :(得分:0)

我认为,首先应该命令,使用,然后选择新的PostObject作为回报