【问题标题】:How to solve error in LINQ: Data is null or empty如何解决 LINQ 中的错误:数据为空或为空
【发布时间】:2016-09-22 07:27:29
【问题描述】:

我有以下代码。我想用 if 条件检查结果是否为null,但它总是显示错误。如何解决?

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 to Entities 不支持 LINQ 表达式节点类型“ArrayIndex”。

【问题讨论】:

    标签: c# asp.net 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();
    

    【讨论】:

      【解决方案2】:

      这应该可以解决您的问题:

      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

      【讨论】:

        【解决方案3】:

        你可以试试这个:

         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();
        

        【讨论】:

          猜你喜欢
          • 2021-09-16
          • 1970-01-01
          • 2015-07-12
          • 2022-10-14
          • 2016-02-04
          • 1970-01-01
          • 1970-01-01
          • 2020-03-05
          • 1970-01-01
          相关资源
          最近更新 更多