Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested LINQ query, error message

Tags:

c#

asp.net

linq

I am currently trying to do a nested query in linq, but I am receiving an error message:

var subcatquery = from categories in mydb.Categories
                  where categories.ParentId == null
                  select new
                  {
                      category = categories.Name,
                      subcat = (from sub in mydb.Categories
                                where sub.ParentId == sub.Id
                                select new
                                {
                                    subcatItem = sub.Name,
                                    subcatId = sub.Id
                                 })
                    };

Results View =The type '<>f__AnonymousType0<subcatItem,subcatId>' exists in both 'myapplication.dll' and 'System.Web.dll'

I can't underestand why.. if I remove the sub query and put it on it's own... it's perfectly fine.

the subcat type is a collection<>, is this where the problem is?

    class categoryGroup 
    {
        public string category;
        public Collection<subcategoryGroup> subcat;

    }
    class subcategoryGroup
    {
        public string subcatItem;
        public int subcatId;

    }
like image 706
user1655940 Avatar asked Nov 04 '22 14:11

user1655940


1 Answers

In my project i am using sub query like this it's below

 var data = (from con in dbData.tblPresenters
                        where con.PresenterID == ID
                        select new
                        {
                            Name = con.Name,

                            Title = dbData.tblTitles.Where(x => x.TitleID == con.PresenterTitleID).FirstOrDefault()
                        }).ToList();

i think this will help you ....

like image 165
Rajpurohit Avatar answered Nov 15 '22 05:11

Rajpurohit