Linq 查询中的 String.contains

我有以下查询。如果字符串不为空或为空,如何使其成为一个包含字符串的查询


if (!string.IsNullOrWhiteSpace(eventRequestModel.Description))

    {

      if (eventRequestModel.Severity.Count > 0)

      {

        eventsModelList = (from job in _unitOfWork.Repository<Jobs>().Get(j => eventRequestModel.SiteIds.Contains(j.JobId.ToString())).Result

                           join ev in _unitOfWork.Repository<EventLogs>().Get(e => e.Description.Contains(eventRequestModel.Description) && eventRequestModel.Severity.Contains(e.Severity.ToString())).Result on job.JobId equals ev.JobId

                           join pnl in _unitOfWork.Repository<Panels>().Get(el => eventRequestModel.SiteIds.Contains(el.JobId.ToString())).Result on ev.PanelId equals pnl.PanelId

                           orderby ev.TimeStamp descending, ev.EventId descending

                           select new EventsModel

                           {

                             UTCTimeStamp = ev.TimeStamp,

                             EventType = ev.EventType,

                             Description = ev.Description,

                             PanelName = pnl.Name,

                             SiteName = job.Name,

                             ChannelGuid = ev.ChannelGuid,

                             MapGuid = ev.MapGuid,

                             Severity = ev.Severity,

                             LogType = ev.LogType,

                             Data1 = ev.Data1,

                             Data2 = ev.Data2,

                             Data3 = ev.Data3,

                             Data4 = ev.Data4,

                             PanelItemType = ev.PanelItemType,

                             PanelItemId = ev.PanelItemId

                           }).Take(numOfItems).ToList();

      }

慕桂英4014372
浏览 78回答 1
1回答

陪伴而非守候

你的区别是join ev in _unitOfWork.Repository<EventLogs>().Get(e => e.Description.Contains(eventRequestModel.Description) && eventRequestModel.Severity.Contains(e.Severity.ToString()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //^^^^^^^^^^^^^^ Here您可以使用三元运算符来避免if (!string.IsNullOrWhiteSpace(eventRequestModel.Description))检查喜欢,join ev in _unitOfWork.Repository<EventLogs>()&nbsp;.Get(e =>&nbsp;&nbsp; &nbsp; &nbsp; string.IsNullOrWhiteSpace(eventRequestModel.Description) ? true : e.Description.Contains(eventRequestModel.Description)&nbsp; &nbsp; &nbsp; && eventRequestModel.Severity.Contains(e.Severity.ToString()))你的整个代码看起来像,if (eventRequestModel.Severity.Count > 0)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; eventsModelList = (from job in _unitOfWork.Repository<Jobs>().Get(j => eventRequestModel.SiteIds.Contains(j.JobId.ToString())).Result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;join ev in _unitOfWork.Repository<EventLogs>().Get(e => string.IsNullOrWhiteSpace(eventRequestModel.Description) ? true : e.Description.Contains(eventRequestModel.Description)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Your answer is here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&& eventRequestModel.Severity.Contains(e.Severity.ToString())).Result on job.JobId equals ev.JobId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;join pnl in _unitOfWork.Repository<Panels>().Get(el => eventRequestModel.SiteIds.Contains(el.JobId.ToString())).Result on ev.PanelId equals pnl.PanelId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;orderby ev.TimeStamp descending, ev.EventId descending&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select new EventsModel&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Your code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}).Take(numOfItems).ToList();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; eventsModelList = (from job in _unitOfWork.Repository<Jobs>().Get(j => eventRequestModel.SiteIds.Contains(j.JobId.ToString())).Result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;join ev in _unitOfWork.Repository<EventLogs>().Get(e => string.IsNullOrWhiteSpace(eventRequestModel.Description) ? true : e.Description.Contains(eventRequestModel.Description)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Your answer is here&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&& eventRequestModel.Severity.Contains(e.Severity.ToString())).Result on job.JobId equals ev.JobId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;join pnl in _unitOfWork.Repository<Panels>().Get(el => eventRequestModel.SiteIds.Contains(el.JobId.ToString())).Result on ev.PanelId equals pnl.PanelId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;orderby ev.TimeStamp descending, ev.EventId descending&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select new EventsModel&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Your code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}).Take(numOfItems).ToList();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP