分享

左联接中的错误空引用

 印度阿三17 2019-10-27

当我评论菜肴时.添加(新菜品{DishID = 8,DishName =“ Name”,DishTypeID = 2,IngredientID = 2});我进去了一项Amount =“ 1 cup” DishID = 1 Ingridient =“ egg” Name =“ Soup”.当取消注释该行上升错误时,b.IngredientTypeID中的null引用异常.主要问题如何获得两项:

1)Amount =“ 1 cup” DishID = 1 Ingridient =“ egg” Name =“ Soup”

2)Amount = null DishID = 2 Ingridient = null名称= 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; }
}
}

解决方法:

问题在于,由于DefaultIfEmpty,b,c,t变量中的任何一个都可能为null,因此您需要在任何成员访问权限(包括连接条件)中考虑该变量.

如果您使用的是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 };
来源:https://www./content-4-525901.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多