Entity Framework include children that grand children are null

不打扰是莪最后的温柔 提交于 2019-12-24 08:39:09

问题


Seem like a simple one, but not coming up with the right thing. All that I need to do is get a count of child items that have it's own child null. Here is what I have been working with:

var data = await _context.award.Include(a => a.expenses.Select(p => p.invoice == null)).ToListAsync();

I have also tried other combinations here with no luck. The error I get is

InvalidOperationException: The property expression 'a => {from expense p in [a].expenses select ([p].invoice == null)}' is not valid. The expression should represent a property access: 't => t.MyProperty'.

I change it to match and it just triggers a new error.

I just want to get a list of award with it's list of expenses listed (fine with just the .ID if that influences the solution) where the invoice parent object is not set and is null.

UPDATE requested models

public class invoice
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [ForeignKey("INV_NUM_ForeignKey")]
    public invoice_number fin_invoice_number { get; set; }
}

public class invoice_number
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    public int number { get; set; }

    public invoice invoice { get; set; }

    public string display { get { return string.Format("sps-{0}", (new String('0', 6) + number.ToString())).Substring(number.ToString().Count()-7, number.ToString().Count()); } }
}

回答1:


You have to use .Include together with .ThenInclude. Docs explains it clearly here (Including multiple levels).

var data = await _context.award
    .Include(a => a.expenses)
        .ThenInclude(e => e.invoice)
    .ToListAsync();

Notice: But notice, that ThenInclude has two overloads and chances are big, that Visual Studio will select the wrong one or just display one (wrong one) and give you either errors while typing or do not offer autocompetition for e if e is not a collection. If you ignore the error and type the correct property and close the bracket, the error will disappear.




回答2:


It seems you know what you are doing but sometimes it happens and a magic letter solves nightmare problems...

Generally (Which version of EF you use dunno), as far as i know, like described here

https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx

Check your model has related properties Decide Eagerly or Lazy ...

If thesee are not solution then switch your computer :).. Then Just in EF configuration check relation definitions

Sorry still can not comment out.I had to write answer...




回答3:


Try re-writing your code like this

var data = await _context.award.Include(a => a.expenses).Where(p => p.expenses.Any(a => a.invoice == null)).ToListAsync();


来源:https://stackoverflow.com/questions/42547524/entity-framework-include-children-that-grand-children-are-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!