2016-09-22 89 views
1

我有以下代碼。我想檢查結果是否爲帶有if條件的null,但總是顯示錯誤。如何解決這個問題?如何解決LINQ中的錯誤:數據爲空或空

string StrRefNo = Request.QueryString["ref"]; 
string[] SMSid = StrRefNo.Split('$'); 

DownloadsDbEntities db = new DownloadsDbEntities(); 
var data = (from d in db.SMSLink_Expiry 
    where d.RefNo == SMSid[0] 
    && d.SMSLink.ID == Convert.ToInt32(SMSid[1]) 
    select d).ToList(); 

if (data.Count > 0) 
{ 
    string ss = "yes"; 
} 

的LINQ表達式節點類型 'ArrayIndex' 在LINQ不支持對實體。

回答

1

由於表達式不能轉換成SQL,拉出來的語句

string SMSId0 = SMSid[0]; 
int SMSId1 = Convert.ToInt32(SMSid[1]); 

var data = (from d in db.SMSLink_Expiry 
      where d.RefNo == SMSId0 
      && d.SMSLink.ID == SMSId1 
      select d).ToList(); 
0

這應該可以解決你的問題:

string StrRefNo = Request.QueryString["ref"]; 
string[] SMSid = StrRefNo.Split('$'); 
var ref = SMSid[0]; 
var smsLinkId = Convert.ToInt32(SMSid[1]); 
DownloadsDbEntities db = new DownloadsDbEntities(); 
var data = (from d in db.SMSLink_Expiry 
      where d.RefNo == ref 
      && d.SMSLink.ID == smsLinkId 
      select d).ToList(); 

if (data.Count > 0) 
{ 
    string ss="yes"; 
} 

有很多事情LINQ的不支持,因爲它將查詢翻譯成SQL內部。

參考:The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities

0

你可以試試這個:

string[] SMSid = StrRefNo.Split('$'); 
var refno=SMSid[0]; 
var id=Convert.ToInt32(SMSid[1]; 

DownloadsDbEntities db = new DownloadsDbEntities(); 
var data = (from d in db.SMSLink_Expiry 
where d.RefNo == refno 
&& d.SMSLink.ID == id) 
select d).ToList(); 
相關問題