Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

c# - The Include path expression must refer to a navigation property defined on the type.in eager loading

I try to include anonymous type like this : I want all incomelist attributes in addition to CompanyTitle ,PeriodTypeName )

 var incomeList = ctx.IncomeLists.Include(i => new
                {
                    CompanyTitle = i.CompanyId.ToString() + "/" + i.Company.CompanyName,
                    PeriodTypeName = i.ListPeriods.Select(lp => lp.PeriodType.PeriodTypeName)
                }).ToList()

My model section like this : enter image description here

but i get the following exception :

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path

The result should be the datasource to Gridview.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot use Include to select data like this. Include is used to load related data. You should load your entities using Include then select what you want. Remember to remove .ToString() from CompanyId. EF will do it for you. Your query should look like this:

var incomeList = ctx.IncomeLists
    .Include(i => i.Company)
    .Include(i => i.ListPeriods.Select(lp => lp.PeriodType))
    .Select(i => new 
    {
        CompanyTitle =  i.CompanyId + "/" + i.Company.CompanyName,
        PeriodTypeNames = i.ListPeriods.Select(lp => lp.PeriodType.PeriodTypeName)
    })
    .ToList();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...