简体   繁体   English

当param不为null Npgsql时,在linq查询中包含where子句

[英]Include where clause on linq query when param is not null Npgsql

I have following method that registers a contact in database, but before register I check the contact exists or not: 我有以下方法在数据库中注册联系人,但在注册之前我检查联系人是否存在:

bool RegisterContact(Contact contactInfo) { 
          bool entityExists =
                        _dbContext.Contacts.FirstOrDefault(
                            p => (p.FilesID.Equals(contactInfo.FilesID))
                                 && (p.EmailAddress ==
                                     (string.IsNullOrEmpty(
                                         contactInfo.EmailAddress)
                                         ? p.EmailAddress
                                         : contactInfo.EmailAddress))
                                 &&
                                 (p.DisplayName ==
                                  (string.IsNullOrEmpty(
                                      contactInfo.DisplayName)
                                      ? p.DisplayName
                                      : contactInfo.DisplayName)));
                                                                          }

this query includes the fields that contain value (not null) in search condition (FilesID, EmailAddress, DisplayName) 此查询包括搜索条件中包含值(非空)的字段(FilesID,EmailAddress,DisplayName)

this technique works fine in MSSQL, today i changed the database manager to PostgreSQL and use Npgsql. 这种技术在MSSQL中运行良好,今天我将数据库管理器改为PostgreSQL并使用Npgsql。

All things work except above linq query, which raises an exception with message of : "could not determine data type of parameter $2" 所有的东西都工作,除了上面的linq查询,它引发了一个异常消息:“无法确定参数$ 2的数据类型”

I was forced to solve it in this way: 我被迫以这种方式解决它:

bool RegisterContact(Contact contactInfo)
{ 
    Contact entityExists = null;

      if (string.IsNullOrEmpty(contactInfo.EmailAddress) &&
                        (string.IsNullOrEmpty(contactInfo.DisplayName)))
                        entityExists =
                            _dbContext.Contacts.FirstOrDefault(
                                p => p.FilesID.Equals(contactInfo.FilesID));

                    if (!string.IsNullOrEmpty(contactInfo.EmailAddress) && string.IsNullOrEmpty(contactInfo.DisplayName))
                        entityExists =
                            _dbContext.Contacts.FirstOrDefault(
                                p =>
                                    p.FilesID.Equals(contactInfo.FilesID) &&
                                    p.EmailAddress == contactInfo.EmailAddress);

                    if (string.IsNullOrEmpty(contactInfo.EmailAddress) && !string.IsNullOrEmpty(contactInfo.DisplayName))
                        entityExists =
                            _dbContext.Contacts.FirstOrDefault(
                                p =>
                                    p.FilesID.Equals(contactInfo.FilesID) &&
                                    p.DisplayName == contactInfo.DisplayName);


                    if (!string.IsNullOrEmpty(contactInfo.EmailAddress) &&
                        !string.IsNullOrEmpty(contactInfo.DisplayName))
                        entityExists =
                            _dbContext.Contacts.FirstOrDefault(
                                p =>
                                    p.FilesID.Equals(contactInfo.FilesID) &&
                                    p.EmailAddress == contactInfo.EmailAddress &&
                                    p.DisplayName == contactInfo.DisplayName);

}

Is this Npgsql bug or by design? 这是Npgsql错误还是设计错误? any known solutions/workarounds for the problem? 该问题的任何已知解决方案/解决方法?

I currently have the same cases. 我目前有同样的情况。 I think the problem is the lack of recognition, by NpgSQL, of string.IsNullOrEmpty. 我认为问题是NpgSQL缺乏对string.IsNullOrEmpty的认可。

I replaced the test with a check on empty string, always recognizing as not NULL the input parameter. 我用空字符串检查替换了测试,始终将输入参数识别为非NULL。

-- bad - 不好

        var data = from art in _ctx.Set<Soleo.Model.DLAR>()
                   from iva in _ctx.Set<Soleo.Model.DLAI>().Where(k => k.DITTA == art.DITTA && k.COD == art.CIVA).DefaultIfEmpty()
                   from fam in _ctx.Set<Soleo.Model.DLFA>().Where(k => k.DITTA == art.DITTA && k.COD == art.FAM).DefaultIfEmpty()
                   from mar in _ctx.Set<Soleo.Model.DLMA>().Where(k => k.DITTA == art.DITTA && k.COD == art.MAR).DefaultIfEmpty()
                   from udm in _ctx.Set<Soleo.Model.DLUM>().Where(k => k.DITTA == art.DITTA && k.COD == art.UM).DefaultIfEmpty()
                   where art.DITTA == DLAUTH.Config.Current.DITTA && art.COD.Contains(sel_cod) && art.DES.Contains(sel_des)
                   && (string.IsNullOrEmpty(sel_fam) || string.Compare(art.FAM, sel_fam, true) == 0)
                   && (string.IsNullOrEmpty(sel_mar) || string.Compare(art.MAR, sel_mar, true) == 0)
                   && (art.DIS >= sel_dis_da && art.DIS <= sel_dis_a)
                   select new
                   {
                       COD = art.COD,
                       DES = art.DES,
                       DES_UDM = udm.DES,
                       DES_MAR = mar.DES,
                       DES_FAM = fam.DES,
                       DES_CIVA = iva.DES,
                       MAG1 = art.MAG1,
                       MAG2 = art.MAG2,
                       DES_DIS = art.DIS == 1 ? "Si" : "No"
                   };

-- good: - 好:

            var data = from art in _ctx.Set<Soleo.Model.DLAR>()
                   from iva in _ctx.Set<Soleo.Model.DLAI>().Where(k => k.DITTA == art.DITTA && k.COD == art.CIVA).DefaultIfEmpty()
                   from fam in _ctx.Set<Soleo.Model.DLFA>().Where(k => k.DITTA == art.DITTA && k.COD == art.FAM).DefaultIfEmpty()
                   from mar in _ctx.Set<Soleo.Model.DLMA>().Where(k => k.DITTA == art.DITTA && k.COD == art.MAR).DefaultIfEmpty()
                   from udm in _ctx.Set<Soleo.Model.DLUM>().Where(k => k.DITTA == art.DITTA && k.COD == art.UM).DefaultIfEmpty()
                   where art.DITTA == DLAUTH.Config.Current.DITTA && art.COD.Contains(sel_cod) && art.DES.Contains(sel_des)
                   && (string.Compare(sel_fam, "", true) == 0 || string.Compare(art.FAM, sel_fam, true) == 0)
                   && (string.Compare(sel_mar, "", true) == 0 || string.Compare(art.MAR, sel_mar, true) == 0)
                   && (art.DIS >= sel_dis_da && art.DIS <= sel_dis_a)
                   select new
                   {
                       COD = art.COD,
                       DES = art.DES,
                       DES_UDM = udm.DES,
                       DES_MAR = mar.DES,
                       DES_FAM = fam.DES,
                       DES_CIVA = iva.DES,
                       MAG1 = art.MAG1,
                       MAG2 = art.MAG2,
                       DES_DIS = art.DIS == 1 ? "Si" : "No"
                   };

But I do not think this is the solution. 但我不认为这是解决方案。 I will report the case to NpgSQL. 我将向NpgSQL报告此案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM