linq to sql 案例查询
我在使用 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;
}
*/
}
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用扩展方法比 LINQ 语法更容易做到这一点:
这将在您进行过程中构建 LINQ 表达式。 评估会被延迟,直到您实际调用一些需要执行选择的扩展(例如 Count() 或 ToList()),或者枚举结果的元素。
This would be easier to do with extension methods than LINQ syntax:
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.