問題: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 };
