开发者

Subsonic load objects by a list of ids

开发者 https://www.devze.com 2023-01-24 15:06 出处:网络
Is it possible to load objects by a list of ids using subsonic ActiveRecord? My code looks like: IList<Video> videos = Video.Find(v => videoIds.Contains(v.ID));

Is it possible to load objects by a list of ids using subsonic ActiveRecord? My code looks like:

IList<Video> videos = Video.Find(v => videoIds.Contains(v.ID));

I get an exception: The method 'Contains' is not supported

Do I do something wrong ... or I hit one of开发者_Python百科 subsonic's limitations?

Thanks, Radu


After more research I found a way to achieve this:

 List<int> videoIds = new List<int>(){1, 2, 3, 4, 5};
 SqlQuery query = new Select().From<Video>().Where("ID").In(videoIds);
 List<Video> videos = query.ExecuteTypedList<Video>();


FYI: SubSonic's linq parser does not like generic Lists and Contains

// does not work
List<int> videoIds = new List<int>() {1,2,3,4,5};
var videos = Video.Find(v => videoIds.Contains(v.ID));

// should work
IEnumerable<int> videoIds = new List<int>() {1,2,3,4,5};
var videos = Video.Find(v => videoIds.Contains(v.ID));

Noticed the difference?

Sounds strange, but whenever you want to use Contains() with Subsonic, you first have to cast your List to an IEnumerable to prevent the NotSupportedException.


This is the one-liner I believe you where looking for.

var colMatchingVideos = Video.Find( objVideo => colVideoIds.Any( iVideoId => objVideo.ID == iVideoId).ToList();

Also I would strongly advise you avoid using string literals for columns, instead you could use Video.Columns.Id or expressions Where( o => o.Id). This would ensure that if you change your column names in the Database a compile time exception will occur. Help's a lot with maintainability.

0

精彩评论

暂无评论...
验证码 换一张
取 消