嵌套的LINQ查询,错误消息

时间:2012-10-05 04:25:47

标签: c# asp.net linq

我目前正在尝试在linq中执行嵌套查询,但是我收到一条错误消息:

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
                                 })
                    };

结果视图= The type '<>f__AnonymousType0<subcatItem,subcatId>' exists in both 'myapplication.dll' and 'System.Web.dll'

我不能不知道为什么..如果我删除子查询并把它放在它自己的...它完全没问题。

subcat类型是collection<>,问题出在哪里?

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

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

    }

2 个答案:

答案 0 :(得分:1)

在我的项目中,我正在使用这样的子查询,它位于

之下
 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();

我认为这会对你有帮助....

答案 1 :(得分:1)

您是否尝试定义/命名类型而不是匿名,

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