Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

c# - How to use ORDER BY COUNT in LINQ and SELECT COUNT it?

I have a query and I don't know change it to LINQ

select distinct m.id,m.name, sch.id as schedule, COUNT(tk.id) as tiketSold
from movies m, schedules sch, tickets tk
where m.id = sch.movie_id and sch.id = tk.schedule_id
group by m.id,m.name, sch.id
order by COUNT(tk.id) desc

I'm trying:

var hotMovie = from m in _db.movies
                           from sch in _db.schedules
                           from tk in _db.tickets
                           where m.id == sch.movie_id && sch.id == tk.schedule_id
                           group m by m.id into g
                           orderby g.Count()
                           select new { Movie = g};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I do not have your database, so, I have created 3 classes like your table as I can anticipate. Then I have created 3 list like you table in the "TestMethod". In the linq query, I have joined the 3 list as you shown in sql query segment "where m.id = sch.movie_id and sch.id = tk.schedule_id" and then I perform the group by, order by an select. Here is my code, please try it and let me know it works or not.

 public class movies
{
    public int id { get; set; }
    public string name { get; set; }
 
}

public class schedules
{
    public int id { get; set; }
    public int movie_id { get; set; }
}

public class tickets
{
    public int id { get; set; }
    public int schedule_id { get; set; }
}
 void TestMethod()
 {
        //Add Movies to the list
        List<movies> moviesItems = new List<movies>();
        moviesItems.Add(new movies() { id = 1, name = "A" });
        moviesItems.Add(new movies() { id = 2, name = "B" });

        //Add Schedules to the list
        List<schedules> schedulesItems = new List<schedules>();
        schedulesItems.Add(new schedules() { id = 1, movie_id = 1 });
        schedulesItems.Add(new schedules() { id = 2, movie_id = 2 });
        schedulesItems.Add(new schedules() { id = 3, movie_id = 1 });
        schedulesItems.Add(new schedules() { id = 4, movie_id = 2 });

        //Add Tickets to the list
        List<tickets> ticketsItems = new List<tickets>();
        ticketsItems.Add(new tickets() { id = 1, schedule_id = 1 });
        ticketsItems.Add(new tickets() { id = 2, schedule_id = 1 });
        ticketsItems.Add(new tickets() { id = 3, schedule_id = 2 });
        ticketsItems.Add(new tickets() { id = 4, schedule_id = 2 });
        ticketsItems.Add(new tickets() { id = 5, schedule_id = 2 });
        ticketsItems.Add(new tickets() { id = 6, schedule_id = 3 });
        ticketsItems.Add(new tickets() { id = 7, schedule_id = 3 });
        ticketsItems.Add(new tickets() { id = 8, schedule_id = 3 });
        ticketsItems.Add(new tickets() { id = 9, schedule_id = 3 });
        ticketsItems.Add(new tickets() { id = 10, schedule_id = 4 });

        var query = from final in (from m in moviesItems
                                   join sch in schedulesItems on m.id equals sch.movie_id
                                   join tk in ticketsItems on sch.id equals tk.schedule_id
                                   select new { movieID = m.id, movieName = m.name, schID = sch.id, tkID = tk.id })
                    group final by new { final.movieID, final.movieName, final.schID } into g
                    orderby g.Count() descending
                    select new { g.Key.movieID, g.Key.movieName, g.Key.schID, tiketSold = g.Count() };
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...