Linq to Sql转换每次都给null

时间:2014-06-03 11:11:09

标签: c# sql linq sql-server-2008-r2

我有SQL查询:

 select * from EH_PP_TeacherObservations tos
 join account acc on tos.EH_PP_TeacherAcctId = acc.id
join EH_PP_ObserverStatus os on tos.EH_PP_TeacherAcctId = os.EH_PP_AcctId 
join [dbo].[EH_PP_TeacherObservationStatusesRefTable] tosrt on tos.[EH_PP_TOSRT_TeacherObservationStatusIDEH] = tosrt.[EH_PP_TOSRT_TeacherObservationStatusID]
where tos.[EH_PP_TOSRT_TeacherObservationStatusIDEH] = 'A717732D-68FA-47FE-A354-C2CB589F29FA' and os.[EH_PP_O_isObserver]= 1

我想将其转换为LINQ。

我试过如下:

 list = (

                         from a in context.EH_PP_TeacherObservations
                         join b in context.accounts on a.EH_PP_TeacherAcctId equals b.id
                         join c in context.EH_PP_ObserverStatus on a.EH_PP_TeacherAcctId equals c.EH_PP_AcctId
                         join d in context.EH_PP_TeacherObservationStatusesRefTables on a.EH_PP_TOSRT_TeacherObservationStatusIDEH equals d.EH_PP_TOSRT_TeacherObservationStatusID
                         where a.EH_PP_TOSRT_TeacherObservationStatusIDEH == new Guid("A717732D-68FA-47FE-A354-C2CB589F29FA")
                         && c.EH_PP_O_isObserver == true
                       select new Entity.Observations
                       {
                           FullName = (b.firstName + " " + b.lastName),
                           EH_PP_ObservationID = Guid.Parse(a.EH_PP_ObservationID.ToString()),
                           EH_PP_TE_TeacherEvalID = Guid.Parse(a.EH_PP_TE_TeacherEvalID.ToString()),
                           EH_PP_TOT_ObservationStartDateTime = a.EH_PP_TOT_ObservationStartDateTime,
                           EH_PP_TOT_ObservationEndDateTime = a.EH_PP_TOT_ObservationEndDateTime,
                           EH_PP_TOT_Announced = int.Parse(a.EH_PP_TOT_Announced.ToString()),
                           EH_PP_TOT_ObservationNum = a.EH_PP_TOT_ObservationNum.ToString(),
                           EH_PP_TeacherAcctId = a.EH_PP_TeacherAcctId.ToString(),
                           EH_PP_ObserverAcctID = Guid.Parse(a.EH_PP_ObserverAcctID.ToString()),
                           EH_PP_TOSRT_TeacherObservationStatusIDEH = a.EH_PP_TOSRT_TeacherObservationStatusIDEH.ToString()
                       }
                    ).ToList<Entity.Observations>();

但它显示我无效。

请帮帮我。

1 个答案:

答案 0 :(得分:1)

问题出在where语句中: a.EH_PP_TOSRT_TeacherObservationStatusIDEH不等于新Guid(“A717732D-68FA-47FE-A354-C2CB589F29FA”) 尝试Convert.ToString(a.EH_PP_TOSRT_TeacherObservationStatusIDEH).ToUpper()=“A717732D-68FA-47FE-A354-C2CB589F29FA”

注意:Guid是struct,因此在C#中比较struct是一种痛苦,即使你的对象具有相同的值,equals也总是返回false。