Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.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左连接问题_C#_.net_Linq - Fatal编程技术网

C# Linq左连接问题

C# Linq左连接问题,c#,.net,linq,C#,.net,Linq,我已经看到了很多关于这个的例子,但是在这个例子中重新生成它们似乎不起作用。有人知道下面的代码有什么问题吗 var products = new[] { new {ProductName ="Soda", Category = "Beverages"}, new {ProductName ="Tuna", Category = "SeaFood"}, new {ProductName ="Jam", Category = "Cond

我已经看到了很多关于这个的例子,但是在这个例子中重新生成它们似乎不起作用。有人知道下面的代码有什么问题吗

    var products = new[]
    { 
        new {ProductName ="Soda", Category = "Beverages"},
        new {ProductName ="Tuna", Category = "SeaFood"},
        new {ProductName ="Jam", Category = "Condiment"}
    };

    var categories = new[]
    { 
        new {Category = "Beverages", Description="Slurp"},
        new {Category = "SeaFood" , Description="Nosh"},
        new {Category = "Exotic" , Description="Spicy!"},
    };

    var q = from c in categories
                 join p in products on c.Category equals p.Category into tmp
                 from prd in tmp.DefaultIfEmpty()
                 select new { Category = c.Category, 
                              Description = c.Description,        
                              ProductName = prd.ProductName };
非常感谢


Keith

问题在于,“外来”类别中没有产品,因此在尝试读取产品名称(在最后一行代码中)时会抛出
NullReferenceException

为了使代码不崩溃,可以添加空检查:

var q = from c in categories
        join p in products on c.Category equals p.Category into tmp
        from prd in tmp.DefaultIfEmpty()
        select new
        {
            Category = c.Category,
            Description = c.Description,
            ProductName = prd != null ?  prd.ProductName : "[null]"
        };