Linq-to-Sql query advice please

Posted by Mantorok on Stack Overflow See other posts from Stack Overflow or by Mantorok
Published on 2010-05-13T08:21:41Z Indexed on 2010/05/13 8:44 UTC
Read the original article Hit count: 275

Filed under:

Hi all

Just wondering if this is a good approach, I need to search for items containing all of the specified keywords in a space-delimted string, is this the right approach this?

            var result = (from row in DataContext.PublishedEvents
                      join link in DataContext.PublishedEvent_EventDateTimes on row.guid equals link.container
                      join time in DataContext.EventDateTimes on link.item equals time.guid
                      orderby row.EventName
                      select new {row, time});

        // Split the keyword(s) to limit results with all of those words in.
        foreach(var keyword in request.Title.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
        {
           var val = keyword;
           result = result.Where(row=>row.row.EventName.Contains(val));
        }

        var end = result.Select(row=>new EventDetails
                                {
                                   Title = row.row.EventName,
                                   Description = TrimDescription(row.row.Description),
                                   StartDate = row.time.StartDate,
                                   EndDate = row.time.EndDate,
                                   Url = string.Format(ConfigurationManager.AppSettings["EventUrl"], row.row.guid)
                                });

        response.Total = end.Count();
        response.Result = end.ToArray();

Is there a slick Linq-way of doing all of this in one query?

Thanks

© Stack Overflow or respective owner

Related posts about linq-to-sql