C# 实体框架,为什么我的where被忽略了?

C# 实体框架,为什么我的where被忽略了?,c#,entity-framework-5,C#,Entity Framework 5,关于下面的问题,为什么我的条件在哪里 注意:注释掉的sql语句是我在这个linq查询中试图复制的语句 public static List<Sys.Entities.Hms206> Hms206Get( DateTime startDate, DateTime endDate, string courseNumber, bool sOnly ) { //select distinct // p.DEPTID [Rc], // p.NAME [Emp

关于下面的问题,为什么我的条件在哪里

注意:注释掉的sql语句是我在这个linq查询中试图复制的语句

public static List<Sys.Entities.Hms206> Hms206Get( DateTime startDate, DateTime endDate, string courseNumber, bool sOnly ) {
    //select distinct
    //    p.DEPTID [Rc],
    //    p.NAME [EmployeeName],
    //    p.EMPLID [EmployeeId],
    //    x.XLATSHORTNAME [Ran]
    //from 
    //    tablep p
    //    inner join tablex x on p.CM_RAN = x.FIELDVALUE
    //    inner join tablet t on p.EMPLID = t.EMPLID
    //where
    //    p.DEPTID not like '%R'
    //    and p.EMPL_STATUS in('A','L','P','S','W')
    //    and t.COURSE_END_DT between '7/1/1960' and '12/27/2012' 
    //    and t.COURSE = 'C00005' 
    //    and t.ATTENDANCE = 'C' 
    //    and x.FIELDNAME = 'CM_RAN'

    //    and p.CM_IND = 'S' 
    //    --and p.CM_IND in ('S','C') -- all
    //order by 
    //    p.DEPTID, p.NAME
    string[] stats = new string[]{"A","L","P","S","W"};
    using ( var context = new Sys.EntityModels.LCEntities() ) {
        var query = ( from p in context.PsCmSummaries
                        join x in context.TableX on p.CM_RAN equals x.FIELDVALUE
                        join t in context.TableT on p.EMPLID equals t.EMPLID
                        where !p.DEPTID.EndsWith( "R" )
                        && stats.Contains( p.EMPL_STATUS )
                        && t.COURSE_END_DT >= startDate && t.COURSE_END_DT <= endDate
                        && t.COURSE == courseNumber
                        && t.ATTENDANCE == "C"
                        && x.FIELDNAME == "CM_Ran"
                        select new {
                            p.DEPTID,
                            p.NAME,
                            p.EMPLID,
                            x.XLATSHORTNAME,
                            p.CM_IND
                        } );
       // this conditional where is not being applied
       if ( sOnly ) {
            query.Where( x => x.CM_IND == "S" );
        }
        else {
            query.Where( x => x.CM_IND == "S" || x.CM_IND == "C" );
        }

        return query.Distinct().OrderBy( x => x.DEPTID ).ThenBy( x => x.NAME )
            .Select( 
                x => new Sys.Entities.Hms206 { 
                    Rc = x.DEPTID, EmployeeName = x.NAME, EmployeeId = x.EMPLID, 
            Rank = x.XLATSHORTNAME, S_Indicator = x.CM_IND 
            })
            .ToList();
    }
}
您正在调用Where,但完全忽略了返回值-这使得调用毫无用处。你想要的是:

if (sOnly) {
    query = query.Where(x => x.CM_IND == "S");
}
else {
    query = query.Where(x => x.CM_IND == "S" || x.CM_IND == "C");
}
LINQ调用(如Where、Select等)不会修改现有查询-它们通过使用新特性组合上一个查询来返回新查询。

您正在调用Where,但完全忽略返回值-这使得调用无效。你想要的是:

if (sOnly) {
    query = query.Where(x => x.CM_IND == "S");
}
else {
    query = query.Where(x => x.CM_IND == "S" || x.CM_IND == "C");
}

LINQ调用(如Where、Select等)不会修改现有查询-它们通过使用新特性组合以前的查询来返回新查询。

您可以尝试以下方法来替换您的条件:

if ( sOnly ) {
    query.Where( x => x.CM_IND == "S" );
}
else {
    query.Where( x => x.CM_IND == "S" || x.CM_IND == "C" );
}
答复如下:


您可以尝试以下方法来替换您的条件:

if ( sOnly ) {
    query.Where( x => x.CM_IND == "S" );
}
else {
    query.Where( x => x.CM_IND == "S" || x.CM_IND == "C" );
}
答复如下:


谢谢你关于使用三元运算符的好建议。没问题,我正要用@Jon Skeet的答案来建议。谢谢你关于使用三元运算符的好建议。没问题,我正要用@Jon Skeet的答案来建议