Search code examples
linq-to-sqlodata

How to write Sub query using odata


I have two tables Menu and GroupMenu. I need to write sub query in Odata. I am able to get results in SQL for e.g.

select * from [Menu] where id in (select  MenuId from GroupMenu where GroupId = 2)

below is the result for the sameenter image description here

Below is the structure of my class

 [Serializable]
public class GroupMenu
{
    [Key]
    public int Id { get; set; }
    public int GroupId { get; set; }
    [ForeignKey("GroupId")]
    public virtual Group Group { get; set; }
    public int MenuId { get; set; }
    [ForeignKey("MenuId")]
    public virtual Menu Menu { get; set; }
    public DateTime ModifiedDate { get; set; }
}

[Serializable]
public class Group
{
    [Key]
    public int Id { get; set; }

    [MaxLength(80)]
    [Required]
    public string Name { get; set; }

    public bool IsActive { get; set; }

    public DateTime ModifiedOn { get; set; }
    public Guid ModifiedBy { get; set; }
}

Solution

  • Can you post your edmx ($metadata)? "subquery" have to be made via navigation properties. something like that:

    [...]GroupMenus?$filter=GroupId eq 2&$expand=Menu&$select=Menu
    

    -it gives you GroupMenu with included Menu