5

我目前正在尝试在 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类型是 a ,这collection<>是问题所在吗?

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

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

    }
4

2 回答 2

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
                                  })
                     };
于 2012-10-05T14:58:34.330 回答
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();

我想这会对你有所帮助....

于 2012-10-05T05:01:58.343 回答