使用c#在linq查询where条件中的Case语句
本文关键字:Case 语句 条件 where linq 查询 使用 | 更新日期: 2023-09-27 18:11:20
我需要where语句的基本结果,但我正在获得所有记录,如何在linq where条件中给出case语句。如果空数据为给定给搜索意味着它应该搜索那个字段。
我的c#代码 var TicketDetails = (from issuecases in caseModelDatabase.IssueCases
where ( DbFunctions.TruncateTime(issuecases.CreatedDate) >= DbFunctions.TruncateTime(IssueDetails.StartDate)
&& DbFunctions.TruncateTime(issuecases.CreatedDate) <= DbFunctions.TruncateTime(IssueDetails.ToDate)
|| ((!string.IsNullOrEmpty(IssueDetails.TicketArea) && issuecases.TicketArea.Equals(IssueDetails.TicketArea))
|| (!string.IsNullOrEmpty(IssueDetails.TicketType) && issuecases.TicketType.Equals(IssueDetails.TicketType))
|| (!string.IsNullOrEmpty(IssueDetails.Status) && issuecases.Status.Equals(IssueDetails.Status))))
select new {
issuecases.WorkRequestId,
issuecases.Summary,
issuecases.Status,
issuecases.CreatedDate,
issuecases.UpdatedDate
}).ToList();
检查下面的语法生成测试用例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CaseModelDatabase caseModelDatabase = new CaseModelDatabase();
cIssueDetails IssueDetails = new cIssueDetails();
var TicketDetails = (from issuecases in caseModelDatabase.IssueCases
where (DbFunctions.TruncateTime(issuecases.CreatedDate) >= DbFunctions.TruncateTime(IssueDetails.StartDate)
&& DbFunctions.TruncateTime(issuecases.CreatedDate) <= DbFunctions.TruncateTime(IssueDetails.ToDate))
where (string.IsNullOrEmpty(IssueDetails.TicketArea) ? true : issuecases.TicketArea == IssueDetails.TicketArea)
where (string.IsNullOrEmpty(IssueDetails.TicketType) ? true : issuecases.TicketType == IssueDetails.TicketType)
where (string.IsNullOrEmpty(IssueDetails.Status) ? true : issuecases.Status == IssueDetails.Status)
select new
{
issuecases.WorkRequestId,
issuecases.Summary,
issuecases.Status,
issuecases.CreatedDate,
issuecases.UpdatedDate
}).ToList();
}
public class CaseModelDatabase
{
public List<cIssueCases> IssueCases { get; set; }
}
public class cIssueCases
{
public DateTime CreatedDate { get; set; }
public string TicketArea { get; set; }
public string TicketType { get; set; }
public string Status { get; set; }
public string Summary { get; set; }
public int WorkRequestId { get; set; }
public DateTime UpdatedDate { get; set; }
}
public class cIssueDetails
{
public DateTime StartDate { get; set; }
public DateTime ToDate { get; set; }
public string TicketArea { get; set; }
public string TicketType { get; set; }
public string Status { get; set; }
}
public static class DbFunctions
{
public static DateTime TruncateTime(DateTime time)
{
return time;
}
}
}
}
试试这个
var TicketDetails = (from issuecases in caseModelDatabase.IssueCases
where ( (DbFunctions.TruncateTime(issuecases.CreatedDate) >= DbFunctions.TruncateTime(IssueDetails.StartDate)
&& DbFunctions.TruncateTime(issuecases.CreatedDate) <= DbFunctions.TruncateTime(IssueDetails.ToDate))
|| ((!string.IsNullOrEmpty(IssueDetails.TicketArea) && issuecases.TicketArea.Equals(IssueDetails.TicketArea))
|| (!string.IsNullOrEmpty(IssueDetails.TicketType) && issuecases.TicketType.Equals(IssueDetails.TicketType))
|| (!string.IsNullOrEmpty(IssueDetails.Status) && issuecases.Status.Equals(IssueDetails.Status))))
select new {
issuecases.WorkRequestId,
issuecases.Summary,
issuecases.Status,
issuecases.CreatedDate,
issuecases.UpdatedDate
}).ToList();