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# 新的空白、非空匿名类型_C#_Linq - Fatal编程技术网

C# 新的空白、非空匿名类型

C# 新的空白、非空匿名类型,c#,linq,C#,Linq,我需要在linq查询返回的其他匿名类型列表中插入一个新的空白但非空的匿名类型。可能吗?我只能得到空值 var something = ( from a in x.As where x != null join b in x.Bs on a.key equals b.key select new { a.prop1, a.prop2, b.prop1, b.prop2, b.prop3 }).ToList(); // insert blank

我需要在linq查询返回的其他匿名类型列表中插入一个新的空白但非空的匿名类型。可能吗?我只能得到空值

var something =
( from a in x.As
 where x != null
  join b in x.Bs
    on a.key equals b.key
select new
{
    a.prop1,
    a.prop2,
    b.prop1,
    b.prop2,
    b.prop3
}).ToList();

// insert blank
//something.InsertRange(0, something.DefaultIfEmpty());
//something.InsertRange(0, something.Take(0));
//?

我不知道如何在单个查询中执行此操作,因为匿名类型的默认值为
null
。我要做的是预先创建一个“默认”项,并在必要时附加它:

var blank = new {
                prop1 = default(string),  // can't use null 
                prop2 = default(string),  // because the type cannot be inferred
                prop3 = default(string),
                prop4 = default(string)
                };

var something = /*...*/.ToList();
if(!something.Any())
    something.Add(blank);
请注意,只要字段名匹配(在名称和类型中)
blank
将与Linq查询创建的匿名类型相同