Linq to entity - 加入多个条件

时间:2013-03-30 18:36:03

标签: entity-framework linq-to-entities

我正在使用Entity Framwork,我有下一个投影:

        var items = from mapsDsc in DbContext.MapsDesc
                    join mapCat in DbContext.MapsCategories on mapsDsc.MapID equals mapCat.MapID
                    join mainCat in DbContext.MainCategories on mapCat.MainCategory equals mainCat.MainCatID
                    join subCat in DbContext.SubCategories on mapCat.SubCategory equals subCat.SubCatID
                    select new DataModel.ComplexEntities.MapsDescExt
                    {
                        MapID = mapsDsc.MapID,
                        MapName = mapsDsc.MapName,
                        MapLink = mapsDsc.MapLink,
                        Note = mapsDsc.Note,
                        MainCategoryID = mapCat.MainCategory,
                        MainCategoryName = mainCat.Category,
                        SubCategoryID = mapCat.SubCategory,
                        SubCategoryName = subCat.Category
                    };

请注意,这个投影填充强类型而不是匿名的。 我需要稍微自定义这个投影,但我不知道如何。

在我上次加入时,我需要使用“或”运算符实现下一个条件:

join subCat in DbContext.SubCategories on mapCat.SubCategory equals subCat.SubCatID or subCat.SubCatID equals "0"

我该怎么办?我看到的所有示例都与匿名类型有关,对我来说并不好。 感谢

1 个答案:

答案 0 :(得分:0)

你没有拥有来使用连接语法,您可以通过手册where执行相同操作,然后在额外条件下滑动:

var items = from mapsDsc in DbContext.MapsDesc
            join mapCat in DbContext.MapsCategories on mapsDsc.MapID
                                                equals mapCat.MapID
            join mainCat in DbContext.MainCategories on mapCat.MainCategory
                                                 equals mainCat.MainCatID
            from subCat in DbContext.SubCategories
/* here */  where mapCat.SubCategory == subCat.SubCatID || subCat.SubCatID == "0"
            select new DataModel.ComplexEntities.MapsDescExt
            {
            ...

(注意mapCat.SubCategory应该是一个id字段,你可能知道哪个。)