linq到sql case查询 [英] linq to sql case query

查看:147
本文介绍了linq到sql case查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试最终做的是基于这个伪代码表达式。

  public IQueryable< CTest> searchRecords(string category,string searchString,DateTime startDate,DateTime endDate,int searchType,int searchType2)
{
// - 搜索所有记录
// - 从信息表中
// - 由包含特定搜索的类别列
// - 另由
//如果同时输入startDate和endDate(不是(0000,00,00)OR null),则获取记录
//通过这个表达式
//〜否则不用担心这个语句
// - 还有
//如果一个searchType等于零(0)然后从
// search_type表搜索记录等于零(0)
//〜如果一个searchType等于1(1),然后从
// search_type表中搜索记录等于一(1)
//〜否则不用担心这个语句
// - 还有
//〜如果一个searchType2是等于零(0)然后从
// search_type表搜索记录等于零(0)
//〜如果searchType2等于1(1)然后从
搜索记录// search_type表等于一(1)
//〜否则不用担心这个语句

//这是我尝试的
/ *
var table = db.table1;

switch(category)
{
case_category1:
var records =
from c in table
其中c.column1。 ToString()。包含(searchString)
选择新的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;
默认值:
break;
}
* /
}

提前感谢。 / p>

解决方案

这将比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);
}

...

这将建立LINQ表达你一起去评估延迟,直到您实际调用需要执行选择的一些扩展名,如Count()或ToList(),或枚举结果的元素。


Im having problems building a query with the linq to sql data query expression in c#.

What I'm trying to ultimately do is based on this pseudo-code expression.

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;
                }
             */
        }

Thanks in advance.

解决方案

This would be easier to do with extension methods than LINQ syntax:

 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 );
 }

 ...

This will build up the LINQ expression as you go along. The evaluation is delayed until you actually invoke some extension that requires the selection to be performed, like Count() or ToList(), or enumerate the elements of the result.

这篇关于linq到sql case查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆