Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq to SQL:忽略WHERE子句中的空参数_C#_Sql_Linq_Linq To Sql_Lambda - Fatal编程技术网

C# Linq to SQL:忽略WHERE子句中的空参数

C# Linq to SQL:忽略WHERE子句中的空参数,c#,sql,linq,linq-to-sql,lambda,C#,Sql,Linq,Linq To Sql,Lambda,下面的查询应返回具有在ownergroupId中提供的匹配Id或与ownerUserId匹配的记录。但是如果ownerUserId为空,我希望忽略查询的这一部分 public static int NumberUnderReview(int? ownerUserId, List<int> ownerGroupIds) { return ( from c in db.Contacts where

下面的查询应返回具有在ownergroupId中提供的匹配Id或与ownerUserId匹配的记录。但是如果ownerUserId为空,我希望忽略查询的这一部分

    public static int NumberUnderReview(int? ownerUserId, List<int> ownerGroupIds)
    {
        return ( from c in db.Contacts
                 where 
                 c.Active == true 
                 &&
                 c.LastReviewedOn <= DateTime.Now.AddDays(-365) 
                 &&
                 ( // Owned by user
                    !ownerUserId.HasValue || 
                    c.OwnerUserId.Value == ownerUserId.Value
                 )
                 &&
                 ( // Owned by group
                    ownerGroupIds.Count == 0 ||
                    ownerGroupIds.Contains( c.OwnerGroupId.Value )
                 )
                 select c ).Count();
    }
public static int numberrunderreview(int?ownerUserId,列出ownerGroupId)
{
返回(来自数据库中的c触点
哪里
c、 活动==真
&&

c、 LastReviewDon您是否有一些OwnerUserId为null的联系人?如果是,则
c.OwnerUserId
可以为null,并且在
c.OwnerUserId.value

中没有任何值,那么有条件地将where子句添加到表达式树中如何

public static int NumberUnderReview(int? ownerUserId, List<int> ownerGroupIds)
    {

    var x = ( from c in db.Contacts
                 where 
                 c.Active == true 
                 &&
                 c.LastReviewedOn <= DateTime.Now.AddDays(-365) 
                 &&
                 ( // Owned by group
                    ownerGroupIds.Count == 0 ||
                    ownerGroupIds.Contains( c.OwnerGroupId.Value )
                 )
                 select c );

    if (ownerUserId.HasValue) {
        x = from a in x
            where c.OwnerUserId.Value == ownerUserId.Value
    }

    return x.Count();
    }
public static int numberrunderreview(int?ownerUserId,列出ownerGroupId)
{
变量x=(从数据库中的c开始)
哪里
c、 活动==真
&&

c、 LastReviewedOn您的问题是您没有传递可为null的int,而是传递了null

试试这个:

Print(null);

private void Print(int? num)
{
     Console.WriteLine(num.Value);
}
你会得到同样的错误

如果您这样做,它应该工作:

var q = ( from c in db.Contacts
                 where 
                 c.Active == true 
                 &&
                 c.LastReviewedOn <= DateTime.Now.AddDays(-365) 
                 &&
                 ( // Owned by group
                    ownerGroupIds.Count == 0 ||
                    ownerGroupIds.Contains( c.OwnerGroupId.Value )
                 )
                 select c );

if(ownerUserId != null && ownerUserId.HasValue)
     q = q.Where(p => p.OwnerUserId.Value == ownerUserId.Value);

return q.Count();
var q=(从数据库中的c开始)
哪里
c、 活动==真
&&
c、 p.OwnerUserId.Value==OwnerUserId.Value)的最新版本;
返回q.Count();
问题:&&“和”| |被转换为类似于“AndCondition(a,b)”的方法,因此“!a.HasValue | | | a.Value==b”变为“或条件(!a.HasValue,a.Value==b)”,其原因可能是为了得到一个通用的解决方案,使其同时适用于代码和SQL语句。因此,请使用“:”符号

有关更多信息,请参阅我的博客帖子:

//新修订的代码。
公共静态int NumberUnderReview(int?ownerUserId,列出OwnerGroupId)
{
返回(来自数据库中的c触点
哪里
c、 活动==真
&&

c、 上一次查看它看起来是正确的。它一定是抱怨的
c.OwnerUserId
。你能确认吗?我已将该子句更改为
(c.OwnerUserId.HasValue&&OwnerUserId.HasValue&&c.OwnerUserId.Value==OwnerUserId.Value)
要进行测试,我仍然看到相同的错误消息。任何
c.OwnerGroupId
是否可以为null,从而引发异常?看起来这个问题比我最初想象的要复杂:+1。另外:如果您查询userId=3,而某个联系人没有userId,您将执行
(.OutruleSerID.HasValue.C.OrruleUrID.Value=OnSeruReID.Value)<代码> >代码>({ 3 } HasValue { { } } NUL}}值= { 3 }值)<代码> >访问<代码> {NULL}。value < /Cord>抛出异常。我没有考虑的好点,但是我将子句改为<代码>。(c.OwnerUserId.HasValue&&OwnerUserId.HasValue&&c.OwnerUserId.Value==OwnerUserId.Value)
进行测试,但仍看到相同的错误消息
// New revised code.
public static int NumberUnderReview(int? ownerUserId, List<int> ownerGroupIds)
{
    return ( from c in db.Contacts
             where 
             c.Active == true 
             &&
             c.LastReviewedOn <= DateTime.Now.AddDays(-365) 
             &&
             ( // Owned by user
                // !ownerUserId.HasValue || 
                // c.OwnerUserId.Value == ownerUserId.Value
                ownerUserId.HasValue ? c.OwnerUserId.Value == ownerUserId.Value : true
             )
             &&
             ( // Owned by group
                // ownerGroupIds.Count == 0 ||
                // ownerGroupIds.Contains( c.OwnerGroupId.Value )
                ownerGroupIds.Count != 0 ? ownerGroupIds.Contains( c.OwnerGroupId.Value ) : true
             )
             select c ).Count();
}