在Linq中如何应用案例

时间:2018-01-17 06:44:09

标签: c# entity-framework linq

我有一个Linq查询,可以使用实体框架从数据库中获取数据。我必须将给定的sql condetion应用于linq查询。我的示例sql查询是

 DECLARE @EmrId NVARCHAR(50)
 set @EmrId='784197621725304'
SELECT 
   [ftp_imptlg_ImprtTyp_id],[ftp_imptlg_daypath],[ftp_imptlg_subfolderpath]
  ,[ftp_imptlg_emirates_id],[ftp_imptlg_srcfile_name],[ftp_imptlg_filereadon]
  ,[ftp_imptlg_opr_status],[ftp_imptlg_doc_no],[ftp_imptlg_emp_id]
  ,[ftp_imptlg_upld_filename] ,[ftp_imptlg_doctype_id]
FROM [C3KYC].[dbo].[ftp_import_logs] AS c
inner join tm_doc_type as d on c.[ftp_imptlg_doctype_id]=d.doc_typeid
inner join tm_import_type as e on c.[ftp_imptlg_ImprtTyp_id]=e.imprttype_id

WHERE c.ftp_imptlg_emirates_id = case when @EmrId ='0' then 
 c.ftp_imptlg_emirates_id else @EmrId end  
AND 
c.ftp_imptlg_opr_status='failed'
and d.doc_typeid=6
and e.imprttype_id='2'

我尝试了下面的linq查询

string strEmrid = EmiratesId != null ? EmiratesId : "0";
        using (var db = new DB_KYC3Entities())
        {
            db.Configuration.ProxyCreationEnabled = false;
            List<ImportLogDetails> listofLogDetails = (from c in db.ftp_import_logs
                                                       join d in db.tm_doc_type on c.ftp_imptlg_doctype_id equals d.doc_typeid
                                                       join e in db.tm_import_type on c.ftp_imptlg_ImprtTyp_id equals e.imprttype_id
                                                       where c.ftp_imptlg_ImprtTyp_id == ImportTypeId
                                                       && c.ftp_imptlg_emirates_id == strEmrid
                                                       && c.ftp_imptlg_opr_status== "Failed"
                                                       && d.doc_typeid== 6
                                                       && e.imprttype_id== "2"
                                                       select new ImportLogDetails
                                                       {
                                                           ImportType = e.imprtTye_name,
                                                           SourcePath = c.ftp_imptlg_subfolderpath,
                                                           DateOfAction = c.ftp_imptlg_filereadon,
                                                           DocumentType = d.doctype_name,
                                                           EmiratesId = c.ftp_imptlg_emirates_id,
                                                           Status = c.ftp_imptlg_opr_status,
                                                           KycEmployeeId = c.ftp_imptlg_emp_id,
                                                           DocumentTypeId = c.ftp_imptlg_doctype_id
                                                       }).ToList();

            return listofLogDetails;

1 个答案:

答案 0 :(得分:0)

您可以在LINQ中使用?:运算符,

 x.Where( x => x.ftp_imptlg_emirates_id == (
       x.ftp_imptlg_emirates_id == "0" ? "0" : x.ftp_imptlg_emirates_id
 ))

您的查询,

string strEmrid = EmiratesId != null ? EmiratesId : "0";
    using (var db = new DB_KYC3Entities())
    {
        db.Configuration.ProxyCreationEnabled = false;
        List<ImportLogDetails> listofLogDetails = (from c in db.ftp_import_logs
            join d in db.tm_doc_type on c.ftp_imptlg_doctype_id equals d.doc_typeid
            join e in db.tm_import_type on c.ftp_imptlg_ImprtTyp_id equals e.imprttype_id
            where c.ftp_imptlg_ImprtTyp_id == ImportTypeId
            && 

           c.ftp_imptlg_emirates_id == (strEmirId == "0" ? c.ftp_imptlg_emirates_id : strEmirid)


            && c.ftp_imptlg_opr_status== "Failed"
            && d.doc_typeid== 6
            && e.imprttype_id== "2"
            select new ImportLogDetails
            {
                ImportType = e.imprtTye_name,
                SourcePath = c.ftp_imptlg_subfolderpath,
                DateOfAction = c.ftp_imptlg_filereadon,
                DocumentType = d.doctype_name,
                EmiratesId = c.ftp_imptlg_emirates_id,
                Status = c.ftp_imptlg_opr_status,
                KycEmployeeId = c.ftp_imptlg_emp_id,
                DocumentTypeId = c.ftp_imptlg_doctype_id
            }).ToList();

    return listofLogDetails;