使用Linq to Sql更新Sub查询

时间:2014-03-02 14:23:50

标签: c# asp.net linq

我有SQL query这里有两个表tblImagesSPEC

我从tblImages中选择了必填列,之后更新了SPEC

update SPEC
    set image1 = isnull((select Image_name
                         from tblimages
                         where orderby = 1 and
                            doc_type=2 and
                            main_ID=@new_ID),
                        'noimage.png')
    where ID=@new_ID

如何使用Linq to Sql转换此查询?

1 个答案:

答案 0 :(得分:1)

你无法在单个查询中执行此操作:

var img = db.tblimages.Where(u => u.orderby == 1
                  u.doc_type == 2 && 
                  u.main_ID == @new_ID)
              .Select(u => u.Image_name)
              .FirstOrDefault();

var o = db.SPEC.FirstOrDefault(u => u.ID == @new_ID);

o.image1 = img != null ? img : "noimage.png"

db.SaveChanges();