Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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列表检查null时出现问题_C#_Linq - Fatal编程技术网

C# Linq列表检查null时出现问题

C# Linq列表检查null时出现问题,c#,linq,C#,Linq,根据下面的数据,我试图编写一个LINQ语句,它将使用(grp2.Select(y=>y.SubscriptionId)==null)检查SubscriptionId是否为null。这一定是失败了,因为它总是进入else部分并返回,4,这不是我要找的。这是检查此值是否为null的正确方法吗 LINQPad示例 class Subscription { public int? SubscriptionId { get; set; }

根据下面的数据,我试图编写一个LINQ语句,它将使用
(grp2.Select(y=>y.SubscriptionId)==null)
检查SubscriptionId是否为null。这一定是失败了,因为它总是进入else部分并返回
,4
,这不是我要找的。这是检查此值是否为null的正确方法吗

LINQPad示例

    class Subscription
        {
            public int? SubscriptionId { get; set; }
            public int ParentProductId { get; set; }
            public string ParentProductName { get; set; }
            public string ChildProductName { get; set; }
            public int ChildProductId { get; set; }
            public int GroupId { get; set; }
            public DateTime? EndDate { get; set; }
        }
        class SubscriptionViewModel
        {
            public int SubscriptionId { get; set; }
            public int ParentProductId { get; set; }
            public string ParentProductName { get; set; }
            public string SubscriptionIds { get; set; }
            public int GroupId { get; set; }

        }
        class SubscriptionChildViewModel
        {
            public string ChildProductName { get; set; }
            public int ChildProductId { get; set; }
        }

       

void Main()
{
                List<Subscription> ListOfSubscription = new List<Subscription>();
                ListOfSubscription.Add(new Subscription() { SubscriptionId = null, ParentProductId = 4, ChildProductId = 4, ParentProductName = "Product 1", ChildProductName = "Product 1", GroupId = 362, });
        
                ListOfSubscription.Add(new Subscription() { SubscriptionId = 2, ParentProductId = 114, ChildProductId = 1, ParentProductName = "Product 2", ChildProductName = "Product 3", GroupId = 1,  });
                ListOfSubscription.Add(new Subscription() { SubscriptionId = 3, ParentProductId = 114, ChildProductId = 2, ParentProductName = "Product 2", ChildProductName = "Product 4", GroupId = 1,   });

                var groupedSubscriptions = ListOfSubscription.GroupBy(u => u.GroupId);

                var result = groupedSubscriptions.Select(grp1 => new
                {
                    GroupId = grp1.Key,
                    Subscriptions = grp1.GroupBy(subscr => new
                    {
                        subscr.ParentProductId,
                        subscr.ParentProductName,
                    })
                    .Select(grp2 => new SubscriptionViewModel
                    {
                        GroupId = grp1.Key,
                        ParentProductId = grp2.Key.ParentProductId,
                        ParentProductName = grp2.Key.ParentProductName,
                        SubscriptionIds = (grp2.Select(y => y.SubscriptionId) == null) ? null : (string.Format("{0},{1}", string.Join(",", grp2.Select(y => y.SubscriptionId)), grp2.Key.ParentProductId))
                })
                });

                var x = result.SelectMany((s => s.Subscriptions));
        
                Console.Write(x);
}
类订阅
{
公共int?SubscriptionId{get;set;}
public int ParentProductId{get;set;}
公共字符串ParentProductName{get;set;}
公共字符串ChildProductName{get;set;}
public int ChildProductId{get;set;}
public int GroupId{get;set;}
公共日期时间?结束日期{get;set;}
}
类SubscriptionViewModel
{
公共int SubscriptionId{get;set;}
public int ParentProductId{get;set;}
公共字符串ParentProductName{get;set;}
公共字符串下标{get;set;}
public int GroupId{get;set;}
}
类SubscriptionChildViewModel
{
公共字符串ChildProductName{get;set;}
public int ChildProductId{get;set;}
}
void Main()
{
List ListOfSubscription=新列表();
添加(新订阅(){SubscriptionId=null,ParentProductId=4,ChildProductId=4,ParentProductName=“Product 1”,ChildProductName=“Product 1”,GroupId=362,});
添加(新订阅(){SubscriptionId=2,ParentProductId=114,ChildProductId=1,ParentProductName=“产品2”,ChildProductName=“产品3”,GroupId=1,});
添加(新订阅(){SubscriptionId=3,ParentProductId=114,ChildProductId=2,ParentProductName=“Product 2”,ChildProductName=“Product 4”,GroupId=1,});
var groupedSubscriptions=ListOfSubscription.GroupBy(u=>u.GroupId);
var result=groupedSubscriptions.Select(grp1=>new
{
GroupId=grp1.Key,
订阅=grp1.GroupBy(subscr=>new
{
subscr.ParentProductId,
subscr.ParentProductName,
})
.选择(grp2=>new SubscriptionViewModel
{
GroupId=grp1.Key,
ParentProductId=grp2.Key.ParentProductId,
ParentProductName=grp2.Key.ParentProductName,
SubscriptionId=(grp2.Select(y=>y.SubscriptionId)==null)?null:(string.Format(“{0},{1}”,string.Join(“,”,grp2.Select(y=>y.SubscriptionId)),grp2.Key.ParentProductId))
})
});
var x=result.SelectMany((s=>s.Subscriptions));
控制台。写入(x);
}

删除该括号y.SubscriptionId)

删除该括号y.SubscriptionId)

这可能适用于您,要使用可空值,您可以使用“HasValue”而不是直接与空值进行比较

SubscriptionId=(grp2.Select(y=>y.SubscriptionId.HasValue)==null)?null:(string.Format(“{0},{1}”,string.Join(“,”,grp2.Select(y=>y.SubscriptionId.HasValue)),grp2.Key.ParentProductId))


Microsoft建议将HasValue用于可空类型

这可能适用于您,要使用可空类型,您可以使用“HasValue”,而不是直接与null进行比较

SubscriptionId=(grp2.Select(y=>y.SubscriptionId.HasValue)==null)?null:(string.Format(“{0},{1}”,string.Join(“,”,grp2.Select(y=>y.SubscriptionId.HasValue)),grp2.Key.ParentProductId))

Microsoft建议对可空类型使用HasValue更改逻辑

(grp2.Select(y=>y.SubscriptionId)==null)

grp2.Where(y=>y.SubscriptionId==null).Count()>0
更改逻辑

(grp2.Select(y=>y.SubscriptionId)==null)

grp2.Where(y=>y.SubscriptionId==null).Count()>0