1

I have the following code. I want to check if the result is null with an if condition but it always shows an error. How to solve this?

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";
}

The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.

Antti29
  • 2,953
  • 12
  • 34
  • 36
Addi Khan
  • 67
  • 8

3 Answers3

1

Since the expression can not be translated into SQL, pull it out of the statement

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();
fubo
  • 44,811
  • 17
  • 103
  • 137
0

This should fix your problem:

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";
}

There are many things Linq does not support, since its translating the query to SQL internally.

For reference: The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities

Community
  • 1
  • 1
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41
0

You can try this :

 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();
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36