案例声明在linq查询使用c#的where条件

时间:2015-09-04 07:12:32

标签: c# linq

我需要在where语句的基础上得到结果,但我得到所有的记录,如何在linq中给出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();

2 个答案:

答案 0 :(得分:0)

试试这个

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

答案 1 :(得分:0)

检查下面的syntac make test case

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