左连接中的空引用错误

时间:2016-03-18 20:56:42

标签: c# linq

当我发表评论dishes.Add(new Dishes { DishID = 8, DishName = "Name", DishTypeID = 2, IngredientID = 2 });时,我会进入ll一个项目Amount="1 cup" DishID=1 Ingridient="egg" Name ="Soup"。取消注释该行引发错误时,b.IngredientTypeID中的空引用异常。如何进入ll两个项目的主要问题是:

1)Amount="1 cup" DishID=1 Ingridient="egg" Name ="Soup"

2)Amount=null DishID=2 Ingridient=null Name =null

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        List<Dishes> dishes = new List<Dishes>();
        List<Ingredients> ingredients = new List<Ingredients>();
        List<Amount> amount = new List<Amount>();
        List<Ingredient> ingredient = new List<Ingredient>();

        dishes.Add(new Dishes { DishID = 1, DishName = "Soup", DishTypeID = 1, IngredientID = 1 });
        //dishes.Add(new Dishes { DishID = 8, DishName = "Name", DishTypeID = 2, IngredientID = 2 }); 
        ingredients.Add(new Ingredients { AmountID = 2, IngredientID = 1, IngredientTypeID = 1, IngredientUniqID = 1 });
        amount.Add(new Amount { AmountID = 2, AmountName = "1 cup" });
        ingredient.Add(new Ingredient { IngredientID = 1, IngredientName = "egg" });

        var test = from dish in dishes
                   join ing in ingredients on dish.IngredientID equals ing.IngredientID into result
                   from b in result.DefaultIfEmpty()
                   join i in ingredient on b.IngredientTypeID equals i.IngredientID into r
                   from c in r.DefaultIfEmpty()
                   join am in amount on b.AmountID equals am.AmountID into s
                   from t in s.DefaultIfEmpty()
                   select new DisplayRecipe { Name = dish.DishName, Amount = t.AmountName, Ingredient = c.IngredientName, DishID = dish.DishID };

        List<DisplayRecipe> ll = test.ToList();
    }
}
public partial class Dishes
{
    public int DishID { get; set; }
    public string DishName { get; set; }
    public Nullable<int> DishTypeID { get; set; }
    public Nullable<int> IngredientID { get; set; }
}

public partial class Ingredients
{
    public int IngredientID { get; set; }
    public Nullable<int> AmountID { get; set; }
    public Nullable<int> IngredientTypeID { get; set; }
    public int IngredientUniqID { get; set; }
}

public partial class Amount
{
    public int AmountID { get; set; }
    public string AmountName { get; set; }
}
public partial class Ingredient
{
    public int IngredientID { get; set; }
    public string IngredientName { get; set; }
}
public class DisplayRecipe
{
    public string Name { get; set; }
    public string Ingredient { get; set; }
    public string Amount { get; set; }
    public int DishID { get; set; }
}
}

2 个答案:

答案 0 :(得分:3)

问题在于bct变量可能因nullDefaultIfEmpty而变为?.。任何会员访问权限,包括加入条件。

如果您使用的是C#6(VS2015),可以像这样使用var test = from dish in dishes join ing in ingredients on dish.IngredientID equals ing.IngredientID into result from b in result.DefaultIfEmpty() join i in ingredient on b?.IngredientTypeID equals i.IngredientID into r from c in r.DefaultIfEmpty() join am in amount on b?.AmountID equals am.AmountID into s from t in s.DefaultIfEmpty() select new DisplayRecipe { Name = dish.DishName, Amount = t?.AmountName, Ingredient = c?.IngredientName, DishID = dish.DishID }; 运算符

var test = from dish in dishes
           join ing in ingredients on dish.IngredientID equals ing.IngredientID into result
           from b in result.DefaultIfEmpty()
           join i in ingredient on b != null ? b.IngredientTypeID : null equals i.IngredientID into r
           from c in r.DefaultIfEmpty()
           join am in amount on b != null ? b.AmountID : null equals am.AmountID into s
           from t in s.DefaultIfEmpty()
           select new DisplayRecipe { Name = dish.DishName, Amount = t != null ? t.AmountName : null, Ingredient = c != null ? c.IngredientName : null, DishID = dish.DishID };

在C#6之前:

context

答案 1 :(得分:1)

问题是你添加了这一行:

dishes.Add(new Dishes { DishID = 8, DishName = "Name", DishTypeID = 2, IngredientID = 2 }); 

但是也没有添加依赖于你的连接的其他行(例子):

ingredients.Add(new Ingredients { AmountID = 2, IngredientID = 2, IngredientTypeID = 1, IngredientUniqID = 1 });
ingredient.Add(new Ingredient { IngredientID = 2, IngredientName = "ham" });

因此,当你的程序试图找到一个2的成分ID时,因为它已被添加到菜肴中,它找不到一个并产生错误。

有效的代码示例:

        dishes.Add(new Dishes { DishID = 1, DishName = "Soup", DishTypeID = 1, IngredientID = 1 });
    dishes.Add(new Dishes { DishID = 8, DishName = "Name", DishTypeID = 2, IngredientID = 2 }); 
    ingredients.Add(new Ingredients { AmountID = 2, IngredientID = 1, IngredientTypeID = 1, IngredientUniqID = 1 });
    ingredients.Add(new Ingredients { AmountID = 2, IngredientID = 2, IngredientTypeID = 1, IngredientUniqID = 1 });
    amount.Add(new Amount { AmountID = 2, AmountName = "1 cup" });
    ingredient.Add(new Ingredient { IngredientID = 1, IngredientName = "egg" });
    ingredient.Add(new Ingredient { IngredientID = 2, IngredientName = "ham" });