ASP.NET Web API 2使用Get方法从表多个参数中搜索数据

问题描述 投票:0回答:1

这是一个代码。我想如果ID值是12位数或将形成13位数搜索并在数据匹配时返回对象值,如果不匹配则返回null。

public MobilinkN get(ulong ID)
        {
            SubInfoNEntities robject = new SubInfoNEntities();
            using (SubInfoNEntities entities = new SubInfoNEntities())
            {
                string scnic = ID.ToString();
                if (scnic.Length == 13)
                {
                    return entities.MobilinkNs.FirstOrDefault(e => e.CNIC == scnic);
                }
                else if(scnic.Length == 12)
                { 
                   return entities.MobilinkNs.FirstOrDefault(e => e.MSISDN ==scnic);
                }
                return entities.MobilinkNs.FirstOrDefault(e => e.MSISDN == scnic);

            }
        }
asp.net api web
1个回答
0
投票
public MobilinkN get(ulong ID)
    {
        SubInfoNEntities robject = new SubInfoNEntities();
        using (SubInfoNEntities entities = new SubInfoNEntities())
        {
            string scnic = ID.ToString();
            if (scnic.Length == 13)
            {
                return entities.MobilinkNs.FirstOrDefault(e => e.CNIC == scnic);
            }
            else if(scnic.Length == 12)
            { 
               return entities.MobilinkNs.FirstOrDefault(e => e.MSISDN ==scnic);
            }
            else
            {
               return null;
            }
           // return entities.MobilinkNs.FirstOrDefault(e => e.MSISDN == scnic);  --> Remove this line

        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.