在 LINQ c# 中添加 NOT EXIST 和 EXIT 逻辑

问题描述 投票:0回答:1

下面是我的 SQL 查询,我正试图转换为 LINQ 面临一些挑战感谢任何帮助

if (@IDOut > 0 and not exists(select top 1'x' from tbl1    
       where Color = 'Red'    
       and VinId = @VinID    
       and ParentId = @ParentID)    
      and exists(select top 1'x' from tbl2 where DetailDesc = @DetailDesc    
       and PayId = 1))    
     insert tbl1(ParentId, Color, VinId, DateEntered,CreatedDateTime,MasterId)    
     values(@ParentID, 'Red', VinID, Getdate(),@IDOut) 

我试过的等效 LINQ

 var isRecordExist=((from t1 in tbl1 where !t1.Color.Equals(ColorEnum.Red)
                      && !t1.VinId==input.VinId
                      && !t1.ParentId==input.ParentId).Any()select t1).ToList()
//Not sure on how to add another query of EXIST 
   If(@IDOut>0 && isRecordExist.Count()>0)
   {
    //blah blah
   }            

  
c# .net entity-framework linq
1个回答
0
投票

你的逻辑不太对。您需要使用

==
而不是
!=
,并将
!
放在外面。而且你不需要
.ToList
你可以在整个事情上使用
.Any
.

var isRecordExist = !tbl1.Any(t1 =>
       t1.Color == ColorEnum.Red
    && t1.VinId == input.VinId
    && t1.ParentId == input.ParentId
   )
  && tbl2.Any(t2 =>
       t2.DetailDesc = input.DetailDesc
    && t2.PayId == 1
   );

if (IDOut > 0 && isRecordExist)
{
    //blah blah
}   
© www.soinside.com 2019 - 2024. All rights reserved.