c# - Filtering by plain date SQL to Linq syntax -


i have following sql , result:

select eventtime incidents -- result -- 2015-10-20 00:00:00.000 2015-10-20 05:00:00.000 2015-10-20 05:50:00.000 2015-10-20 07:00:00.000 2015-10-20 09:00:30.000 2015-10-21 05:00:00.000 2015-10-21 06:10:00.000 2015-10-22 09:10:00.000 

i use following sql filter plain date (sql 2014):

select distinct convert (date, [eventtime]) eventdate incidents -- result -- eventdate 2015-10-20 2015-10-21 2015-10-22 

so easy in sql. linq try logic:

var q = db.timelines.select(x => x.time.date).distinct(); // show q.tolist().foreach(x => { console.writeline("eventdate: {0}", x); }); 

breaks with: additional information: specified type member 'date' not supported in linq...

...how write second sql in linq?

if change query list before using operators aren't translatable sql, work.

var q = db.timelines.tolist.select(x => x.time.date).distinct(); // show q.foreach(x => { console.writeline("eventdate: {0}", x); }); 

but need cast list before date portion of date, not after.

edit:

in case, i'm not sure performance increase @ all, mentioned in comment below juharr, use asenumerable if want query remain lazy.

var q = db.timelines.asenumerable.select(x => x.time.date).distinct(); 

edit 2:

if want run query entirely on sql server, can use dbfunctions.truncatetime translatable function:

var q = db.timelines.select(x => dbfunctions.truncatetime(x.time)).distinct(); 

edit 3: also, sergey right first example, should select want before dumping memory if doing way:

var q = db.timelines.select(x => x.time).asenumerable.select( x => x.date).distinct(); 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -