views:

59

answers:

2

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 });
                    }
                }
+2  A: 

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

Data = (s.Description != null ? s.Description : s.Summary)
RandomNoob
Good point! If they both contain it I will return the Summary
Jon
A: 

I went with option 3

Jon