简体   繁体   English

Linq条件选择带有空成员集合

[英]Linq conditional select with null member collection

I have searched through a lot of similar questions, but cannot find one that solves this problem. 我已经搜索了很多类似的问题,但是找不到解决该问题的方法。 Good chance I am overlooking something obvious, but please suggest how to do this with a single iteration. 我有很好的机会忽略了一些显而易见的事情,但是请提出一个迭代步骤来提出建议。

var dataWithResults =
    from d in data
    where d.QCResults != null
    from q in d.QCResults
    select new QCAMaterial
    {
        Name = q == null ? nodata : q.ComponentName,
        Value = q == null ? nodata : q.Result
    };

var dataWithNullResults =
    from d in data
    where d.QCResults == null
    select new QCAMaterial
    {
        Name = nodata,
        Value = nodata
    };

Since both the classes without QCResults as well as QCResults with no specific data in their properties get the nodata value, you could omit the 'where' and combine them as: 由于两个不类QCResults以及QCResults在它们的属性没有具体的数据得到了nodata值,你可以省略“其中”并将它们组合为:

    IEnumerable<QCResult> dummycol = new[] {new QCResult()}; //assuming QCResult is the class exposed by the QCResults property
    var res = from d in data                
            from q in d.QCResults ?? dummycol
            select new QCAMaterial
            {
                Name = q == null ? nodata: q.ComponentName, //side note: can also be written as q?.ComponentName ?? nodata
                Value = q == null ? nodata : q.Result
            };

If QCResults is empty, a dummy collection is used with a single entry. 如果QCResults为空,则将虚拟集合与单个条目一起使用。 The dummy-collection could be created inline as well, but this should be more efficient. 虚拟集合也可以内联创建,但这应该更有效。

You can do a combination of Enumerable.Empty and Enumerable.DefaultIfEmpty : 您可以结合使用Enumerable.EmptyEnumerable.DefaultIfEmpty

var allData =
            from d in data
            from q in d.QCResults ?? Enumerable.Empty<QCResult>().DefaultIfEmpty()
            select new QCAMaterial
            {
                Name = q == null ? nodata : q.ComponentName,
                Value = q == null ? nodata : q.Result
            };

In case you want to treat an empty list ( QCResults ) the same as a null list, you can modify the query: 如果您想将一个空列表( QCResults )与一个null列表相同,可以修改查询:

var allData =
            from d in data
            from q in (d.QCResults ?? Enumerable.Empty<QCResult>()).DefaultIfEmpty()
            select new QCAMaterial
            {
                Name = q == null ? nodata : q.ComponentName,
                Value = q == null ? nodata : q.Result
            };

You can use a SelectMany: 您可以使用SelectMany:

    var result = data.SelectMany(d => 
                        {
                            return d.QCResults != null
                                ? d.QCResults.Select(q => new QCAMaterial { Name = q.ComponentName, Value = q.Result})
                                : new List<QCAMaterial>{new QCAMaterial { Name = "no data", Value = "no data" }};
                        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM