简体   繁体   English

将SQL查询转换为LINQ(子查询)

[英]Translate SQL query to LINQ (subqueries)

Hi have a site where users can vote on images. 嗨,有一个网站,用户可以在上面对图像进行投票。 The votes are stored in table Votes where I store the Id of the submission (FK_id) 投票存储在表投票中,我在其中存储提交的ID(FK_id)


The tables are: 这些表是:

Table Submissions 表格提交
Id (uniqueidentifier) ID(唯一标识符)
Name (varchar(50)) 名称(varchar(50))
Image (varchar(50)) 图片(varchar(50))

Table Votes 表票
Id (int) ID(int)
Fk_id (foreign key to Submissions.Id) Fk_id(Submissions.Id的外键)

I'm new to linq so I don't know how to translate this: 我是linq的新手,所以我不知道该如何翻译:

SELECT *, 
    isnull((SELECT count(*) 
    FROM Votes
    WHERE Fk_id = S.Id      
    GROUP BY Fk_id),0) as Votes 
FROM Submissions S

I wanted something like this: 我想要这样的东西:

List<Model> = (from p in ????
               select new Model
               {
                   Name = p.Name,
                   Image = p.Image,
                   Votes = p.Votes               
               }).ToList();

Thank you. 谢谢。

Rather than translate your Sql (which I think is overly complicated), don't you just want to do something like : 而不是翻译您的Sql(我认为这过于复杂),您是否只想做类似的事情:

List<Model> = (from p in Submissions 
              select new Model
              {
                 Name = p.Name,
                 Image = p.Image,
                 Votes = p.Votes.Count() 
              }).ToList();

Seems like a fairly straight port to me, other than the order of select and from being reversed. 似乎是一个相当简单的端口对我来说,比内秩序的其他select ,并from被逆转。

Submissions.Select(sub => new
{
  //...other fields
  Votes = Votes.Where(vote => vote.Fk_id == sub.Id)
               .GroupBy(vote => vote.Fk_id)
               .Select(group => group.Count()),
}

Now, looking at the query you could probably just do the group by on Votes directly, rather than as an inner query, and it should be quite a bit more efficient, but that's entirely up to you. 现在,看一下查询,您可能可以直接在Votes上进行分组,而不是作为内部查询,这样做应该会更有效率,但这完全取决于您。

This would be translation of your SQL code, which also takes into account isnull ensuring that there is model for each submission: 这将是您的SQL代码的翻译,它还考虑了isnull确保每个提交都有模型:

List<Model> list = (from s in Submissions
        select new Model
        {
            Id = s.Id,
            Name = s.Name,
            Image = s.Image,
            Votes = (from v in Votes
                     where v.Fk_id == s.Id
                     group v by v.Fk_id into g
                     select g.Count()).FirstOrDefault()
        }).ToList();

If you are using Entity Framework you should ensure that your data model is generated correctly. 如果使用的是Entity Framework ,则应确保正确生成数据模型。 In this case your Submission entity would already contain Votes as property. 在这种情况下,您的Submission实体将已经包含“ Votes作为属性。 Then everything is easy as: 然后一切都很容易,因为:

List<Model> list = (from s in context.Submissions
                    select new Model
                    {
                        Id = s.Id,
                        Name = s.Name,
                        Image = s.Image,
                        Votes = s.Votes.Count()
                    }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM