如何在linq c#中编写以下sql查询,其中某些参数有时会为null
我在sql中有以下查询如何在linq c#中编写以下sql查询,其中某些参数有时会为null,c#,linq,sql-server-2008,C#,Linq,Sql Server 2008,我在sql中有以下查询 select * from dbo.WaitingLists where WaitingListTypeId in (1) or StakeBuyInId in (Select StakeBuyInId from dbo.WaitingLists where StakeBuyInId in (5) and WaitingListTypeId = 2) 在这种情况下,有时StakeBuyInId将为null,或者WaitingListTypeId将为null。我想在下
select * from dbo.WaitingLists
where WaitingListTypeId in (1)
or StakeBuyInId in (Select StakeBuyInId from dbo.WaitingLists where StakeBuyInId in (5) and
WaitingListTypeId = 2)
在这种情况下,有时StakeBuyInId将为null,或者WaitingListTypeId将为null。我想在下面的代码中通过linq c#执行此查询
public GameListItem[] GetMyWaitingList(Guid UserId, int LocalWaitingListTypeId, int GlobalWaitingListTypeId, int[] StakeBuyInIds)
{
ProviderDB db = new ProviderDB();
List<GameListItem> objtempGameListItem = new List<GameListItem>();
List<GameTables> objGameTablesList = new List<GameTables>();
var objWaitingListUser = db.WaitingLists.Where(x => x.UserId.Equals(UserId));
if (LocalWaitingListTypeId > 0 || (GlobalWaitingListTypeId > 0 && StakeBuyInIds != null))
{
objWaitingListUser = objWaitingListUser.Where(x => x.WaitingListTypeId == LocalWaitingListTypeId || (x.WaitingListTypeId == GlobalWaitingListTypeId
&& StakeBuyInIds != null ? StakeBuyInIds.Contains((Int32)x.StakeBuyInId) : true)
);
}
return objtempGameListItem.ToArray();
}
PublicGameListItem[]GetMyWaitingList(Guid用户ID、int LocalWaitingListTypeId、int GlobalWaitingListTypeId、int[]StakeBuyInIds)
{
ProviderDB=新的ProviderDB();
List objtempGameListItem=新列表();
List objGameTablesList=新列表();
var objWaitingListUser=db.WaitingLists.Where(x=>x.UserId.Equals(UserId));
if(localwaitinglistypeid>0 | |(globalwaitinglistypeid>0&&StakeBuyInIds!=null))
{
objWaitingListUser=objWaitingListUser。其中(x=>x.WaitingListTypeId==LocalWaitingListTypeId | |(x.WaitingListTypeId==GlobalWaitingListTypeId
&&StakeBuyInIds!=null?StakeBuyInIds.Contains((Int32)x.StakeBuyInId):true)
);
}
返回objtempGameListItem.ToArray();
}
这里的StakeBuyInIds int[]有时会为null,那么我将如何对上述sql查询执行linq操作。谢谢您的帮助。您可以在表达式之外检查null,如下所示:
if (LocalWaitingListTypeId > 0 || (GlobalWaitingListTypeId > 0 && StakeBuyInIds != null))
{
if (StakeBuyInIds != null)
{
objWaitingListUser = objWaitingListUser.Where(
x => x.WaitingListTypeId == LocalWaitingListTypeId ||
(x.WaitingListTypeId == GlobalWaitingListTypeId &&
StakeBuyInIds.Contains((Int32)x.StakeBuyInId));
} else {
objWaitingListUser = objWaitingListUser.Where(
x => x.WaitingListTypeId == LocalWaitingListTypeId ||
x.WaitingListTypeId == GlobalWaitingListTypeId);
}
}
您也可以这样做:
if (LocalWaitingListTypeId > 0 || (GlobalWaitingListTypeId > 0 && StakeBuyInIds != null))
{
var arrayNull = StakeBuyInIds != null;
var array = StakeBuyInIds ?? new int[0];
objWaitingListUser = objWaitingListUser.Where(
x => x.WaitingListTypeId == LocalWaitingListTypeId ||
(x.WaitingListTypeId == GlobalWaitingListTypeId &&
(arrayNotNull || array.Contains((Int32)x.StakeBuyInId)));
}
它在查询之外测试null,但确保在实际执行查询时它不能为null。在关系对象
WaitingList
中,waitingListTypeId
和stakeBuyinId
应该是可为null的int
List<int?> WaitingListTypeIds=new List(new int?[]{1});
var StakeBuyInIds=from w in WaitingListsCollection where new List<int?>(new int?[]{5}).Contains(w.StakeBuyInId) && w.WaitingListTypeId = 2;
var output= from w in WaitingListsCollection where WaitingListTypeIds.Contains(w.WaitingListTypeId) || StakeBuyInIds.Contains(w.StakebuyInId)
List waitinglistypeids=newlist(newint?[{1});
var StakeBuyInIds=来自WaitingListsCollection中的w,其中新列表(new int?[{5})。包含(w.StakeBuyInId)和&w.WaitingListTypeId=2;
var output=来自WaitingListsCollection中的w,其中WaitingListTypeId.Contains(w.WaitingListTypeId)| | StakebuyInId.Contains(w.StakebuyInId)
但是如果LocalWaitingListTypeId大于0,我需要LocalWaitingListTypeId的数据,如果StakeBuyinId为null,我不想执行该数据或条件linq查询。是否有其他使用“let”或与之相关的解决方案?因为我想优化我的代码。@KomalJariwala好吧,这段代码没有什么低效的地方。我想这比在查询中测试null要详细一些。问题是(我认为)没有将测试数组==null直接转换为SQL的方法,因此在查询表达式中实际上不能这样做。请参阅我的更新答案以了解其他解决方案。