2016-03-18 61 views
3

當我評論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; } 
} 
} 
+0

ingridients和ingridient兩種不同的列表。這是命名不好的問題。 – A191919

回答

3

的問題是,任何bct變量可以null由於DefaultIfEmpty,你需要的任何成員訪問,以說明,其中包括加盟條件。

如果您正在使用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 }; 

而前C#6:

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 }; 
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" });