0

enter image description here Following is my code to load results in a gridview. What I want? I want to be able to modify linq query based on start date,end date and title. User can search on any 1 field. How to make linq query to work for any fields filled? Any ideas are appreciated.

        SPSecurity.RunWithElevatedPrivileges(delegate()
       {
           using (SPSite site = new SPSite(SPContext.Current.Web.Site.WebApplication.Sites[0].Url))
           {
               using (SPWeb web = site.OpenWeb())
               {

                   SPList listCANews = web.Lists["News"];
                   var newsQuery = from SPListItem li in listCANews.Items
                                   orderby li["PublishedDate"] descending
                                   where Convert.ToDateTime(li["PublishedDate"]) <= DateTime.Now
                                   select new
                                   {
                                       Title = li.Title,
                                       Status = li["ArchiveNews"],
                                       StartDate = li["PublishedDate"],
                                       EndDate = li["PublishedEndDate"],
                                       ID = li.ID
                                   };

                   GridViewCANews.DataSource = newsQuery;
                   GridViewCANews.DataBind();
               }
           }
       });
5
  • So if I understand it right you want to make a new query when user clicks on Load Record with the fields he filled out?
    – Gwny
    Aug 17 '15 at 6:14
  • do you mean you want a search based gridview where user can search and search result may be a title,start date,end date or combination of both or all ? Aug 17 '15 at 8:13
  • you should not use this Kind of querying. This way you are retrieving ALL items from the database and do a Linq-Query to the result in Memory. Therefore your Query will take longer with every item that is added to the list. Consider using SPQuery/CAML. Furthermore, you will get Troubles if your list has more than 5000 items in it. De Default-Treshold is 5000 items and the *.Items won´t deliver more than the Treshold. And because you are filtering AFTER you receive the items from database, you won´t get the results you expected Aug 17 '15 at 8:21
  • Correct me if I am wrong. CAML query is better to handle dynamic queries then linq. Aug 17 '15 at 22:30
  • @spMahesh that depends - in my experience CAML syntax quickly gets messy if you need to handle dynamically changing conditions. It certainly is possible to encapsulate each column specifying column name, column type and column value and programmatically build your CAML query but I don't think the scalability is very good with an increasing number of columns
    – Stig Perez
    Feb 16 '16 at 20:03
0

You could try something like Dynamic LINQ which enables you to pass in string expressions instead of strong-typed expressions.

With that said it kind of defeats the purpose of LINQ being strong-typed and you may consider simply getting all items using SPQuery and calling the GetDataTable on the returned SPListItemCollection which also supports filtering and sorting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.