将数据加载到 LINQ 对象以用于搜索结果类型方法的最有效方法

发布于 2024-08-20 18:55:01 字数 2489 浏览 3 评论 0原文

我目前有以下问题:

public IEnumerable<News> NewsItems
        {
            get { return from s in News.All() where s.Description.Contains(SearchCriteria) || s.Summary.Contains(SearchCriteria) select s; }
        }

问题是我只需要返回实际具有数据的一个属性以及 Title 属性,类似于。

 return from s in News.All() where s.Description.Contains(SearchCriteria) || s.Summary.Contains(SearchCriteria) select new {Title = s.Title, Data = //Description or Summary containing the data

如何确定哪一个包含搜索查询?

更新:我有这个,但它显然击中了数据库3次

 var FoundInSummary = News.All().Any(x => x.Summary.Contains(SearchCriteria));

                var FoundInDesc = News.All().Any(x => x.Description.Contains(SearchCriteria));

                IEnumerable<NewsEventSearchResults> result = null;

                if ((FoundInSummary && FoundInDesc) || (FoundInSummary))
                {
                    result = (from s in News.All() where s.Summary.Contains(SearchCriteria) select new NewsEventSearchResults { Title = s.Title, Data = s.Summary, ID = s.ID }).AsEnumerable();
                }
                else if (FoundInDesc)
                {
                    result = (from s in News.All() where s.Description.Contains(SearchCriteria) select new NewsEventSearchResults { Title = s.Title, Data = s.Description, ID = s.ID }).AsEnumerable();
                }

                return result;

更新2:这更有效吗?

var ss = (from s in News.All() where s.Description.Contains(SearchCriteria) || s.Summary.Contains(SearchCriteria) select s).ToList();
                List<NewsEventSearchResults> resultList = new List<NewsEventSearchResults>();
                foreach (var item in ss)
                {
                    bool FoundInSummary = item.Summary.Contains(SearchCriteria);
                    bool FoundInDesc = item.Description.Contains(SearchCriteria);
                    if ((FoundInSummary && FoundInDesc) || (FoundInSummary))
                    {
                        resultList.Add(new NewsEventSearchResults { Title = item.Title, Data = item.Summary, ID = item.ID });
                    }
                    else if (FoundInDesc)
                    {
                        resultList.Add(new NewsEventSearchResults { Title = item.Title, Data = item.Description, ID = item.ID });
                    }
                }

I currently have the following:

public IEnumerable<News> NewsItems
        {
            get { return from s in News.All() where s.Description.Contains(SearchCriteria) || s.Summary.Contains(SearchCriteria) select s; }
        }

The problem is I only need to return the one property that actually has the data as well as the Title property, something similar to.

 return from s in News.All() where s.Description.Contains(SearchCriteria) || s.Summary.Contains(SearchCriteria) select new {Title = s.Title, Data = //Description or Summary containing the data

How do I determine which one contains the search query?

UPDATE: I have this but it obviously hits the DB 3 times

 var FoundInSummary = News.All().Any(x => x.Summary.Contains(SearchCriteria));

                var FoundInDesc = News.All().Any(x => x.Description.Contains(SearchCriteria));

                IEnumerable<NewsEventSearchResults> result = null;

                if ((FoundInSummary && FoundInDesc) || (FoundInSummary))
                {
                    result = (from s in News.All() where s.Summary.Contains(SearchCriteria) select new NewsEventSearchResults { Title = s.Title, Data = s.Summary, ID = s.ID }).AsEnumerable();
                }
                else if (FoundInDesc)
                {
                    result = (from s in News.All() where s.Description.Contains(SearchCriteria) select new NewsEventSearchResults { Title = s.Title, Data = s.Description, ID = s.ID }).AsEnumerable();
                }

                return result;

UPDATE 2: Is this more efficent?

var ss = (from s in News.All() where s.Description.Contains(SearchCriteria) || s.Summary.Contains(SearchCriteria) select s).ToList();
                List<NewsEventSearchResults> resultList = new List<NewsEventSearchResults>();
                foreach (var item in ss)
                {
                    bool FoundInSummary = item.Summary.Contains(SearchCriteria);
                    bool FoundInDesc = item.Description.Contains(SearchCriteria);
                    if ((FoundInSummary && FoundInDesc) || (FoundInSummary))
                    {
                        resultList.Add(new NewsEventSearchResults { Title = item.Title, Data = item.Summary, ID = item.ID });
                    }
                    else if (FoundInDesc)
                    {
                        resultList.Add(new NewsEventSearchResults { Title = item.Title, Data = item.Description, ID = item.ID });
                    }
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偷得浮生 2024-08-27 18:55:01

如果它们包含条件怎么办?或者它们是相互排斥的?如果是这样

Data = (s.Description != null ? s.Description : s.Summary)

What if they both contain the criteria? Or are they mutually exclusive? If so

Data = (s.Description != null ? s.Description : s.Summary)
巡山小妖精 2024-08-27 18:55:01

我选择了选项 3

I went with option 3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文