简体   繁体   中英

LINQ Complex Query

I have a complex SQL query which I need to run in LINQ. I'm wondering is it possible to do it with LINQ, or do I need anything else? Could you please help me? Thanks.

SELECT DISTINCT_PLAY_COUNT,SUM(1) AS CLIENT_COUNT FROM
    (SELECT CLIENT_ID,SUM(1) AS DISTINCT_PLAY_COUNT FROM
        (SELECT CLIENT_ID,SONG_ID FROM PIEXHIBIT 
        WHERE PLAY_TS >= '10/08/2016 00:00:00' AND PLAY_TS <= '10/08/2016 23:59:59'
        GROUP BY CLIENT_ID,SONG_ID
    )
    GROUP BY CLIENT_ID
)
GROUP BY DISTINCT_PLAY_COUNT 

Here is my class for it and what I accomplished so far;

    public class Exhibit
    {
        public string PLAY_ID { get; set; }
        public Int32 SONG_ID { get; set; }
        public Int32 CLIENT_ID { get; set; }
        public DateTime PLAY_TS { get; set; }

    }

    var sql = from Exhibit row in Exhibit
                              where row.PLAY_TS >= DateTime.Parse("10/08/2016 00:00:00") && row.PLAY_TS <= DateTime.Parse("10/08/2016 23:59:59")
                              select new { row.CLIENT_ID, row.SONG_ID };
Exhibits
   .Where(t => t.PLAY_TS >= new DateTime(2016, 8, 10) && t.PLAY_TS < new DateTime(2016, 8, 11))
   .Select(t => new { t.CLIENT_ID, t.SONG_ID })
   .Distinct()
   .GroupBy(c => c.CLIENT_ID)
   .Select(c => c.Count())
   .GroupBy(g => g)
   .Select(g => new { DISTINCT_PLAY_COUNT = g.Key, CLIENT_COUNT = g.Count() })
   .ToList();

I think this should work.

distincts are always a bit tricky with LINQ. It is possible:

Select distinct using linq (I am partial to overloading the equals method, even though that likely isn't the proper way to go.)

The link has a good load of suggestions as to what to do. The "Duplicate answer" thread also has some nice suggestions.

I would also like to suggest that your:

  1. PLAY_TS <= 'DD/MM/YYYY 23:59:59'

should be changed to

  1. PLAY_TS < 'DD+1/MM/YYYY 00:00:00'

Simply because it is possible to actually hit a milisecond issue where you technically didn't change date yet, because the milisecond hasn't hit 999 yet.

I hope you understand what I mean.

I would suggest putting it in a stored procedure instead of using LINQ. Then executing the stored procedure by the code below:

using (var conn = new SqlConnection(connString)) //Connection string in there
using (var command = new SqlCommand("ProcedureNameHere", conn) {
    CommandType = CommandType.StoredProcedure }) {
        conn.Open();
        command.ExecuteNonQuery();
}

Here is my suggestion. GroupBy will automatically give you distinct with my code. :

            var sqlDateTime = from Exhibit row in Exhibit.exhibits
                     where row.PLAY_TS >= DateTime.Parse("10/08/2016 00:00:00") && row.PLAY_TS <= DateTime.Parse("10/08/2016 23:59:59")
                     select new { cid = row.CLIENT_ID, songid = row.SONG_ID, date = row.PLAY_TS };

            var results = sqlDateTime.GroupBy(x => new { cid = x.cid, sid = x.songid })
                .Select(x => new { count = x.Count(), cid = x.Key.cid, sid = x.Key.sid }).ToList();  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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