简体   繁体   English

Linq2sql计数分组投票实体查询问题,一旦分组就无法获取属性?

[英]Linq2sql count grouping vote entity query problems, can't get properties once grouped?

I'm building a music voting application, similar to stack overflow almost. 我正在构建一个音乐投票应用程序,几乎类似于堆栈溢出。

I have 3 tables, Charts, ChartItems, Votes. 我有3个表格,Charts,ChartItems,Vote。

I'm trying to bring back a list of chartitems, which link to a single chart, with the number of votes for each chart item counted. 我正在尝试带回一份图表列表,该列表链接到单个图表,其中包含每个图表项目的投票数。

This is what I am trying currently 这是我目前正在尝试的

var firstList = from chartItem in db.ChartItems
                        join vote in db.Votes on chartItem.ixChartId equals vote.ixChartId into j1
                        where chartItem.ixChartId == id                            
                        from j2 in j1.DefaultIfEmpty()
                        group j2 by chartItem.ixChartItemId into grouped                   
                        select new ChartItemWithVotes
                        {                             
                          totalVotes =  grouped.Count(t => t.ixVoteId != null),

                        //CANT GET OTHER PROPS HERE

                        };

The problem is once I have performed the grouping, I can't get any of the other properties I need from the join to populate the model. 问题是一旦执行了分组,就无法从联接中获得填充模型所需的其他任何属性。 For example each ChartItem should have a title, id etc... 例如,每个ChartItem应该具有标题,ID等。

I have created a ViewModel to hold all the properties that I need, called ChartItemWithVotes(includes all entity values + int totalVotes) 我创建了一个ViewModel来保存我需要的所有属性,称为ChartItemWithVotes(包括所有实体值+ int totalVotes)

Can anyone help me with where I am going wrong. 谁能帮助我解决我的问题。

In the end I am looking for this 最后我正在寻找这个

Chart Name 图表名称

Votes Name 投票名称

20 - ChartItemname 20-ChartItemname

15 - ChartItemname 15-ChartItemname

12 - ChartITemName 12-ChartITemName

This may not be the answer you're looking for, but if the appropriate relationships are set up in SQL server, and therefore have been imported into the DBML, you should be able to do the following: 这可能不是您要寻找的答案,但是,如果在SQL Server中设置了适当的关系,因此已将其导入到DBML中,则您应该能够执行以下操作:

var chartvotes = from chartItem in db.ChartItems
    select new {
    ChartItem = chartItem,
    TotalVotes = chartItem.Votes.Count()
};

I think you might want to create a query resulting in chartItemId and total votes and based on that select all chart items from db.ChartItems. 我认为您可能想创建一个查询结果,得出chartItemId和总票数,并根据该查询从db.ChartItems中选择所有图表项目。 Otherwise it might get unreadable quite quickly. 否则,它可能很快就会变得不可读。 It should not suffer performance-wise to do it like that either. 这样做也不应该影响性能。 Deferred execution in linq should make sure the db only takes a single hit. linq中的延迟执行应确保数据库仅命中一次。

Your design seems to be flawed in that your Votes table has a foreign key of Chart table while it should be pointing to ChartItem table instead. 您的设计似乎存在缺陷,因为您的Votes表具有Chart表的外键,而应该指向ChartItem表。 Then my suggestion would make even more sense and you'd be able to solve everything with a regular GroupBy clause. 然后,我的建议将变得更加有意义,您将能够使用常规的GroupBy子句解决所有问题。

It should be like Chart -> ChartItem -> Votes . 它应该类似于Chart -> ChartItem -> Votes

It looks like your current design is more like ChartItem <- Chart -> Votes 看起来您当前的设计更像是ChartItem <- Chart -> Votes

How about this approach: 这种方法怎么样:

var result = db.Votes
   .Where(v => v.chartItemId != null)
   .GroupBy(v => v.chartItemId)
   .Join(db.ChartItems, v => v.Key, c => c.chartItemId, (v, c) => new {Votes = v.Count(), c.Title, c.Id});

do nested linq queries they work well, I use them in my applications, and since your not using any a where clause thats directly related to your votes table in your main query it should work without any issues. 嵌套linq查询可以很好地工作,我在应用程序中使用它们,并且由于您没有在主查询中不使用与投票表直接相关的where子句,因此它应该可以正常工作。

var firstList = from chartItem in db.ChartItems
                where chartItem.ixChartId == id                            
                select new ChartItemWithVotes
                {
                    chartItemName = chartItem.asName,
                    totalVotes =  (from votes in db.Votes
                                  where vote.ixChart == chartItem.ixChart
                                  select vote.ixVoteId).Count()
                };

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

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