Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My sql statement is

update Gallery set IsPublished = 0 where GalleryId not in ('1','2');

how to convert this into linq

Thanks in advance

See Question&Answers more detail:os

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

1 Answer

You cannot UPDATE in a linq query. Your SELECT query can be something like:

List<int> ids = new List<int>() { 1, 2 }; // Assuming integers here
var galleriesToUpdate = context.Gallery
    .Where(g => !ids.contains(g.GalleryId)).ToList();

And then update them

foreach(var gallery in galleriesToUpdate) {
    gallery.IsPublished = 0;
}

And then save them using the context.

context.SubmitChanges();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...