linq无法创建“匿名类型”类型的常量值;此上下文仅支持基元类型或枚举类型

问题:linq无法创建“匿名类型”类型的常量值;此上下文仅支持基元类型或枚举类型

来源:

            var data = from r in db.ActUserDrawRecord_J
                       join g in db.ActGift_J on r.GiftID equals g.GiftID
                       join u in db.UserInfo on r.UserID equals u.UserID
                       join c in db.ActConfig_J on r.ActID equals c.ActID
                       where string.IsNullOrEmpty(phoneCode) ? true : u.PhoneCode == phoneCode
                       && string.IsNullOrEmpty(stationCode) ? true : r.EStationCode == stationCode
                       && string.IsNullOrEmpty(giftName) ? true : g.GiftName == giftName
                       && r.ActID == actId &&r.State==1
                       orderby r.UseTime descending
                       select new
                       {
                           PhoneCode = u.PhoneCode,
                           GiftName = g.GiftName,
                           UseTime = r.UseTime,
                           EStationCode = r.EStationCode,
                           EAccountType = r.EAccountType,
                           Ephone = r.EPhone,
                           EName = r.EName
                       };

解决方案:

将各个查询条件加()

            var data = from r in db.ActUserDrawRecord_J
                       join g in db.ActGift_J on r.GiftID equals g.GiftID
                       join u in db.UserInfo on r.UserID equals u.UserID
                       join c in db.ActConfig_J on r.ActID equals c.ActID
                       where (string.IsNullOrEmpty(phoneCode) ? true : u.PhoneCode == phoneCode)
                       && (string.IsNullOrEmpty(stationCode) ? true : r.EStationCode == stationCode)
                       && (string.IsNullOrEmpty(giftName) ? true : g.GiftName == giftName)
                       && (r.ActID == actId) &&(r.State==1)
                       orderby r.UseTime descending
                       select new
                       {
                           PhoneCode = u.PhoneCode,
                           GiftName = g.GiftName,
                           UseTime = r.UseTime,
                           EStationCode = r.EStationCode,
                           EAccountType = r.EAccountType,
                           Ephone = r.EPhone,
                           EName = r.EName
                       };

 

posted @ 2017-06-09 17:25  花生打代码会头痛  阅读(2191)  评论(0)    收藏  举报