2012-03-02 114 views
1

我有以下方法創建:LINQ - 不能與WHERE條件

public DataTable.DataTablesPage<DataTable.UserModel> DataTableUserListByAttendees(int geoArea, int CurrentUserID) 
    { 
     var result = from i in _dbContext.Users 
        where i.GeographicalAreas.Any(p => p.GeoAreaID == geoArea) 
        select new DataTable.UserModel() 
        { 
         ID = i.ID, 
         Company = i.Company, 
         DCMember = (i.UserId != null), 
         FirstName = i.FirstName, 
         LastName = i.LastName 
        }; 

}

它工作正常,但它返回不具備的GEOarea媒體資源相關聯的實體集。

public DataTable.DataTablesPage<DataTable.UserModel> DataTableUserListByAttendees(int? geoArea, int CurrentUserID) 
    { 
     var result = from i in _dbContext.Users 
        where i.GeographicalAreas.Any(p => p.GeoAreaID == geoArea) 
        select new DataTable.UserModel() 
        { 
         ID = i.ID, 
         Company = i.Company, 
         DCMember = (i.UserId != null), 
         FirstName = i.FirstName, 
         LastName = i.LastName 
        }; 

}

我如何更改查詢,以便它只會返回他們的GEOarea屬性集實體(又名不爲空)。

回答

4
where (geoArea == null) || i.GeographicalAreas.Any(p => p.GeoAreaID == geoArea) 

或有條件的地方添加到查詢

3

這應該是

where geoArea == null || i.GeographicalAreas.Any(p => p.GeoAreaID == (int)geoArea)