Ask a questionAsk a question
 

AnswerDynamic filters in Linq query

  • Monday, November 02, 2009 9:26 PMsuritta Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I have a linq query.  I want to add where conditions or add filters dynamically to this query and trying to use linq.dynamic or expressions for the first time.  I am getting syntax and complilation errors.  Please help ASAP. What is missing?
    error messages are as follows-
    if (this.myItems[0] != "--") results = CustomSearch<Records>(results, s => s.Business == this.myItems[0]); //<-----NOT WORKING Add Dynamic filters.
    On word CustomSearch- The type argument for method 'ClientPJ.CustomSearch<TKEY>(system.data.dataTable, System.Func<records,bool>)' cannot be inferred from the usage. try specifying the type argument explicitly.

    And on word <Records> Try Type or namespace name <Records> could not be found ( are you missing directive or an assembly reference?)
    public ListItemCollection searchProject(ListItemCollection projList, String searchstr, String columnName)
        {
            DataSet DSToReturn = new DataSet();       
            //ListItem item = projList.FindByText("ProjectName");
            ListItemCollection returnItems = new ListItemCollection();
            DataTable results = (from d in ((DataSet)_MyDataset).Tables["Records"].AsEnumerable()
                                 orderby d.Field<string>("Name") ascending
                                 where (d.Field<string>(columnName) != null)
                                 && d[columnName].ToString().ToLower().Contains(searchstr.ToLower())
                                 select d).CopyToDataTable();
     
            if (this.myItems[0] != "--") results = CustomSearch<Records>(results, s => s.columnName == this.myItems[0]); //  ERROR on CustomSearch<Records>
     
                                   
            
            foreach (ListItem li in projList)
            {
                if ((from System.Data.DataRow row in results.Rows
                     where li.Value.Equals(row["value"].ToString(), StringComparison.InvariantCultureIgnoreCase)
                     select row["value"]).Count() > 0)
                    returnItems.Add(li);
            }
     
            return returnItems;      
           
        }
     
        private DataTable CustomSearch<TKEY>(DataTable dt, Func<Records, bool> selector) //ERROR on <records>
        {
            DataTable results = (dt.AsEnumerable().Where(selector).CopyToDataTable());
            return results;
        }
    

Answers

  • Wednesday, November 04, 2009 8:21 AMZhipeng LeeMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi suritta,

    According to your source code, 'Records' is a table name in '_MyDataset'. However, it's used as a type in your source code. So, have you ever defined a class named 'Records' some where else? If so, you need to include it. If not, you need to define it. There's no such usage in LINQ to Dataset but in LINQ to SQL which works on the premise that you have defined your own DataContext.

    Well, to solve the problem, you can try not to use generic but let the compiler infer from the context.

    Feel free to ask if you have any further question.

    Best regards,
    Charlie Lee

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

All Replies

  • Wednesday, November 04, 2009 8:21 AMZhipeng LeeMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi suritta,

    According to your source code, 'Records' is a table name in '_MyDataset'. However, it's used as a type in your source code. So, have you ever defined a class named 'Records' some where else? If so, you need to include it. If not, you need to define it. There's no such usage in LINQ to Dataset but in LINQ to SQL which works on the premise that you have defined your own DataContext.

    Well, to solve the problem, you can try not to use generic but let the compiler infer from the context.

    Feel free to ask if you have any further question.

    Best regards,
    Charlie Lee

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.