linq投影

时间:2010-11-20 00:41:58

标签: linq

我正在使用join和into语句构建linq查询。

var testmyquery = from uf in TheFolderModel.UserFolders
                where (uf.UserID == TheUserID)
                join ul in TheFolderModel.UserBooksheets on
                uf.FolderID equals ul.FolderID
                join ll in TheFolderModel.BooksheetBooks on
                ul.BooksheetID equals ll.BooksheetID
                join ua in TheFolderModel.BooksAppointments on
                ll.BookID equals ua.BookID
                group ua.BookID by uf.FolderID into Holdingvar
                orderby Holdingvar.Key
                select new { TheCount = Holdingvar.Count(), Holdingvar.Key };

我需要将uf和ul中的表中的列添加到重新调整对象中,但是一旦我在Holdingvar上有一个投影,当我在select new时,intellisense不再允许我访问uf或ul {}声明。如何在select语句中添加列?

感谢。

3 个答案:

答案 0 :(得分:1)

我明白了:

var testmyquery = from uf in TheFolderModel.UserFolders  
                where (uf.UserID == TheUserID)  
                join ul in TheFolderModel.UserBooksheets on  
                uf.FolderID equals ul.FolderID  
                join ll in TheFolderModel.BooksheetBooks on  
                ul.BooksheetID equals ll.BooksheetID  
                join ua in TheFolderModel.BooksAppointments on  
                ll.BookID equals ua.BookID  
                group ua.BookID by uf.FolderID into Holdingvar  
                orderby Holdingvar.Key   
                from uf2 in TheFolderModel.UserFolders
                where uf2.FolderID == testg.Key
                select new { testk = testg.Count(), testg.Key, uf2.FolderName };

我只需要在保持变量中进行投影后重新定义变量。

答案 1 :(得分:0)

你不能......一旦你选择了新的{...,uf和ul超出了范围。

我不确定这是否适合你,但如果你需要计数和密钥,你可以做一个let语句:

var testmyquery = from uf in TheFolderModel.UserFolders
            where (uf.UserID == TheUserID)
            join ul in TheFolderModel.UserBooksheets on
            uf.FolderID equals ul.FolderID
            join ll in TheFolderModel.BooksheetBooks on
            ul.BooksheetID equals ll.BooksheetID
            join ua in TheFolderModel.BooksAppointments on
            ll.BookID equals ua.BookID
            group ua.BookID by uf.FolderID into Holdingvar
            orderby Holdingvar.Key
            let someproperty = new { TheCount = Holdingvar.Count(), Holdingvar.Key }
            select new { uf.UserID, ........ }

答案 2 :(得分:0)

我一直这样做。为什么它不适合你?我的查询结构有点不同。也许你需要改革它?

var q = (from at in Repository.For<AuditTrailEntity>()
                 from att in Repository.For<AuditTrailTypeEntity>()
                 join ue in Repository.For<UserEntity>() on at.UserId equals ue.RowId
                 join up in Repository.For<UserProfileEntity>() on ue.AspnetUserId equals up.UserId
                 where (at.AffectedEntityName == "AssetEntity" && at.PKId == assetId.ToString() && at.ActionType == att.RowId)
                 orderby at.CreatedDate descending
                 select new AssetAuditTrailModel
                            {
                                RowId = at.RowId,
                                AffectedEntityName = at.AffectedEntityName,
                                PKId = at.PKId,
                                ActionType = at.ActionType,
                                ActionData = at.ActionData,
                                UserId = at.UserId,
                                CreatedDate = at.CreatedDate,
                                ActionName = att.FriendlyName,
                                UserName = up.FirstName + " " + up.LastName,
                            });