以IQueryable方式将枚举加入到linq-to-sql查询

时间:2019-06-30 23:34:56

标签: c# entity-framework

是否可以在linq-to-sql语句中加入一个枚举?

尝试使用枚举创建可查询的实体,但似乎EF仍在寻找原始上下文。

            IQueryable<BuildingInspectionType> buildingInspectionTypes = Enum.GetNames(typeof(BuildingInspectionTypes))
                .Select(bit => new BuildingInspectionType
                {
                    Id = (int)Enum.Parse(typeof(BuildingInspectionTypes), bit, true),
                    Description = bit
                }).AsQueryable();
            IQueryable<BuildingInspectionItemStatus> buildingInspectionItemStatus = Enum.GetNames(typeof(BuildingInspectionStatuses))
                .Select(biis => new BuildingInspectionItemStatus
                {
                    Id = (int)Enum.Parse(typeof(BuildingInspectionStatuses), biis, true),
                    Description = biis
                }).AsQueryable();
            IQueryable<IDtoFinderFullBuildingInspection> dataSource = 
                from bi in ctx.BuildingInspections
                join emp in ctx.Contacts on bi.IdInspector equals emp.Id into i
                from insp in i.DefaultIfEmpty()
                join spn in ctx.StrataPlanNumbers.Where(m => m.IsPrimary) on bi.Id equals spn.IdBuilding into bldSpn
                from spn in bldSpn.DefaultIfEmpty()
                join bit in buildingInspectionTypes on bi.IdBuildingInspectionStatus equals bit.Id into bit2
                from bldInsType in bit2.DefaultIfEmpty()
                join bis in buildingInspectionItemStatus on bi.IdBuildingInspectionStatus equals bis.Id into bis2
                from bldInsStatus in bis2.DefaultIfEmpty()
                select new DtoFinderBuildingInspection
                {
                    Id = bi.Id,
                    StrataPlanNo = spn.Description,
                    IdBuilding = bi.IdBuilding,
                    Type = bldInsType.Description,
                    InspectionStatus = bldInsStatus.Description,// Enum.GetName(typeof(BuildingInspectionStatuses), bi.IdBuildingInspectionStatus),
                    Inspector = insp.Name,
                    Notes = bi.Notes
                };

            return dataSource;

我希望可查询的IDtoFinderFullBuildingInspection与两个枚举结合在一起,而不会加入实际实体。

0 个答案:

没有答案