Subsonic 3, SimpleRepository, SQL Server: How to find rows with a null field?

假如想象 提交于 2019-12-10 17:46:09

问题


How ca I use Subsonic's Find<T> method to search for rows with a field containing the "null" value. For the sake of the discussion, let's assume I have a c# class called "Visit" which contains a nullable DateTime field called "SynchronizedOn" and also let's assume that the Subsonic migration has created the corresponding "Visits" table and the "SynchronizedOn" field.

If I was to write the SQL query myself, I would write something like:

SELECT * FROM Visits WHERE SynchronizedOn IS NULL

When I use the following code:

var visits = myRepository.Find<Visit>(x => x.SynchronizedOn == null);

Subsonic turns it into the following SQL query:

SELECT * FROM Visits WHERE SynchronizedOn == null

which never returns any rows.

I tried the following code but it throws an error:

visits = repository.Find<Visit>(x => x.SynchronizedOn.HasValue);

I was able to use the following syntax:

var query = from v in repository.All<Visit>()
            where v.SynchronizedOn == null
            orderby v.CreatedOn
            select v;
visits = query.ToList<Visit>();

but it's not as nice an short as using the Find<T> method.

Anyone knows how I can specify the "SynchronizedOn IS NULL" condition in the Find<T> method?


回答1:


You can use:

var visits = myRepository.All<Visit>().Where(x => x.SynchronizedOn == null).ToList();


来源:https://stackoverflow.com/questions/1249944/subsonic-3-simplerepository-sql-server-how-to-find-rows-with-a-null-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!