Answered by:
Lambda expression used in INCLUDE which is not valid - ASP.net Core mvc

Question
-
User2041008840 posted
Lambda expression used in INCLUDE which is not valid - this error is showing this code
this my codeHow do i solve this I want to select only those record who's dealstatus will be 1
ViewBag.Stages = _context.Stage.Include(a => a.Deals.Where(a=>a.DealStatus == 1)).ToList();
public class Deal { public Deal() { this.DealLosts = new HashSet<DealLost>(); } public int ID { get; set; } [Required] public string Name { get; set; }
public int DealStatus { get; set; } public virtual Stage Stage { get; set; } }public class Stage { public Stage() { this.Deals = new HashSet<Deal>(); } public int ID { get; set; } [Required] public string Name { get; set; } public string BackgroundColor { get; set; } public virtual ICollection<Deal> Deals { get; set; } } }
Friday, November 1, 2019 12:43 PM
Answers
-
User665608656 posted
Hi Prathamesh ,
According to your description, you can try this code :
ViewBag.Stages = _context.Stage.Where(a => a.Deals.Any(c=>c.DealStatus == 1)).ToList();
You can also refer to this link : https://stackoverflow.com/a/3548943
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 5, 2019 3:23 AM
All replies
-
User475983607 posted
You are not following the openly published documentation for querying related entities.
https://docs.microsoft.com/en-us/ef/core/querying/related-data
Friday, November 1, 2019 2:26 PM -
User665608656 posted
Hi Prathamesh,
According to your code , you can try to change your code like this :
ViewBag.Stages = _context.Stage.Include(a => a.Deals.Where(c=>c.DealStatus == 1)).ToList();
You can refer to this link : Loading Related Entities
Best Regards,
YongQing.
Monday, November 4, 2019 6:50 AM -
User2041008840 posted
Not Working like this
Monday, November 4, 2019 12:44 PM -
User665608656 posted
Hi Prathamesh ,
According to your description, you can try this code :
ViewBag.Stages = _context.Stage.Where(a => a.Deals.Any(c=>c.DealStatus == 1)).ToList();
You can also refer to this link : https://stackoverflow.com/a/3548943
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 5, 2019 3:23 AM