linq if语句或存储过程if语句

时间:2012-02-20 08:30:31

标签: c# c linq linq-to-sql

我希望在数据库中运行一段代码。但是没有支持的sql转换(使用linq to sql)。

如何在linq或存储过程中内联转换此代码逻辑?我不知道db和存储过程,所以我最好在linq中编写它。

public Post GetPageOwner(int pageid)
{

    var posts = (from dp in db.Posts where dp.pageid == pageid select dp);
    var returned = posts;

    if (posts.Count() > 0)
    {
        var latest = posts.OrderByDescending(o => o.Date).FirstOrDefault();
        var sharedsamedayaslatest = (from p in posts where p.Date.AddDays(1) >= latest.Date select p);
        if (sharedsamedayaslatest.Count() > 1)
        {
            var followedpost = (from p in posts from s in db.Subscriptions where s.Subscriber == UID && s.Subscribedto == p.UserId select p);
            var count = followedpost.Count();
            if (count == 1)
            {
                returned = followedpost;
            }
            else if (count > 1)
            {
                returned = (from s in followedpost let reposts = GetPostReposts(s.id) let rating = GetPostRating(s.id) let score = reposts + rating orderby score descending select s);
            }
            else
            { 
                //no follower shared this post so return the most liked
                returned = (from s in sharedsamedayaslatest let reposts = GetPostReposts(s.id) let rating = GetPostRating(s.id) let score = reposts + rating orderby score descending select s);
            }
        }
        else
        {
            //no shares on the day the latest share
            returned = sharedsamedayaslatest;
        }
    }
    else
    {
        //only one post
        returned = posts;
    }

    return returned.FirstOrDefault(); //order by userid gets a random one
}

1 个答案:

答案 0 :(得分:0)

这可能是这样的:

        public Post GetPageOwner(int pageid)
    {

        var posts = (from dp in db.Posts where dp.pageid == pageid select dp);
        var returned = posts;

        if (posts.Count() > 0)
        {
            var latest = posts.OrderByDescending(o => o.Date).FirstOrDefault();
            var sharedsamedayaslatest = (posts.Where(p => p.Date.AddDays(1) >= latest.Date));
            if (sharedsamedayaslatest.Count() > 1)
            {
                var followedpost = (posts.SelectMany(p => db.Subscriptions, (p, s) => new {p, s}).Where(
                    @t => s.Subscriber == UID && s.Subscribedto == p.UserId).Select(@t => p));
                var count = followedpost.Count();
                if (count == 1)
                {
                    returned = followedpost;
                }
                else if (count > 1)
                {
                    returned = (followedpost.Select(s => new {s, reposts = GetPostReposts(s.id)}).Select(
                        @t => new {@t, rating = GetPostRating(s.id)}).Select(
                            @t => new {@t, score = reposts + rating}).OrderByDescending(@t => score).Select(@t => s));
                }
                else
                {
                    //no follower shared this post so return the most liked
                    (sharedsamedayaslatest.Select(s => new {s, reposts = GetPostReposts(s.id)}).Select(
                        @t => new {@t, rating = GetPostRating(s.id)}).Select(
                            @t => new {@t, score = reposts + rating}).OrderByDescending(@t => score).Select(@t => s)) = returned;
                }
            }
            else
            {
                //no shares on the day the latest share
                returned = sharedsamedayaslatest;
            }
        }
        else
        {
            //only one post
            returned = posts;
        }

        return returned.FirstOrDefault(); //order by userid gets a random one
    }