Linq to sql linq到sql案例查询

Linq to sql linq到sql案例查询,linq-to-sql,expression,case,Linq To Sql,Expression,Case,我在用c#中的linq to sql数据查询表达式构建查询时遇到问题 我最终要做的是基于这个伪代码表达式 public IQueryable<CTest> searchRecords(string category, string searchString, DateTime startDate, DateTime endDate, int searchType, int searchType2) { //-Search All Records

我在用c#中的linq to sql数据查询表达式构建查询时遇到问题

我最终要做的是基于这个伪代码表达式

public IQueryable<CTest> searchRecords(string category, string searchString, DateTime startDate, DateTime endDate, int searchType, int searchType2)
        {
            //-Search All Records
            //-From the information table
            //-By the category column containing a specific search
            //-Also by
            //  ~if both a startDate and endDate are entered (not (0000,00,00) OR null) then get records
            //  by this expression
            //  ~else then don't worry about this statement
            //-Also by
            //  ~if a searchType is equal zero(0) then search for records from the 
            //  search_type table equal to zero(0)
            //  ~if a searchType is equal one(1) then search for records from the 
            //  search_type table equal to one(1)
            //  ~else then don't worry about this statement
            //-Also by
            //  ~if a searchType2 is equal zero(0) then search for records from the 
            //  search_type table equal to zero(0)
            //  ~if a searchType2 is equal one(1) then search for records from the 
            //  search_type table equal to one(1)
            //  ~else then don't worry about this statement

            //Here is my attempt at it
            /*  
                var table = db.table1;

                switch (category)
                {
                case "_category1":
                    var records =
                        from c in table
                        where c.column1.ToString().Contains(searchString)
                        select new CTest
                        {
                            test_id = c.id,
                            test_name = c.name,
                            test_number = c.number,
                            date = ((startDate != null) && (endDate != null)) ? ((c.test_date >= startDate) && (c.test_date <= endDate)) :
                                         c.test_date),
                            test_type = (searchType == 0 ? (c.searchType = 0) :
                                        searchType == 1 ? (c.searchType = 1) :
                                        c.searchType),
                            test_type2 = (searchType2 == 0 ? (c.searchType2 = 0) :
                                        searchType2 == 1 ? (c.searchType2 = 1) :
                                        c.searchType2)
                        };
                    break;
                default:
                    break;
                }
             */
        }
public IQueryable searchRecords(字符串类别、字符串searchString、DateTime startDate、DateTime endDate、int searchType、int searchType2)
{
//-搜索所有记录
//-从信息表中
//-按包含特定搜索的类别列
//-也由
//~如果同时输入startDate和endDate(不是(0000,00,00)或null),则获取记录
//用这个表达
//~z~否则,别担心这句话
//-也由
//~如果searchType等于零(0),则从
//搜索类型表等于零(0)
//~如果searchType等于一(1),则从
//搜索类型表等于一(1)
//~z~否则,别担心这句话
//-也由
//~如果searchType2等于零(0),则从
//搜索类型表等于零(0)
//~如果searchType2等于一(1),则从
//搜索类型表等于一(1)
//~z~否则,别担心这句话
//这是我的尝试
/*  
var table=db.table1;
开关(类别)
{
案例“_类别1”:
风险值记录=
从表中的c开始
其中c.column1.ToString()包含(searchString)
选择新CTest
{
测试id=c.id,
test_name=c.name,
测试编号=c编号,

date=((startDate!=null)&&(endDate!=null))?((c.test\u date>=startDate)&&(c.test\u date使用扩展方法比使用LINQ语法更容易:

 var records = context.Table
                      .Where( c => c.column1.Contains( searchString ) );
 if (startDate != null && endDate != null)
 {
     records = records.Where( c => c.test_date >= startDate
                                    && c.test_date <= endDate );
 }

 ...
var records=context.Table
。其中(c=>c.column1.Contains(searchString));
if(startDate!=null&&endDate!=null)
{
记录=记录。其中(c=>c.test\u date>=startDate
&&c.测试日期可能的副本