Include where clause on linq query when param is not null Npgsql - c#

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)
this technique works fine in MSSQL, today i changed the database manager to PostgreSQL and use Npgsql.
All things work except above linq query, which raises an exception with message of : "could not determine data type of parameter $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? 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.
I replaced the test with a check on empty string, always recognizing as not NULL the input parameter.
-- 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.

Related

If result is < 0 in select new linq

I'm having the next issue, I have the next code
var creditos = from c in db.creditos
join s in db.solicitudDelCliente on c.SolicitudDelClienteID equals s.ID
join cl in db.cliente on s.ClienteID equals cl.ID
where c.Eliminado != true && cl.Eliminado != true && c.NegocioID == negocio_id
select new
{
c.ID,
cl.NumeroCliente,
c.Consecutivo,
NegocioID = negocio_id,
cl.NombreCompleto,
c.FechaAlta,
c.CapitalPrestado,
c.Vencido,
c.Quebranto,
c.Juridico,
c.Liquidado,
c.Reestructura,
c.Eliminado,
c.Estatus,
CapitalPagado = db.movimientos.Where(mm=>mm.CreditoID == c.ID && mm.Eliminado != true && mm.DepositoMovimientoID != null && mm.CategoriaMovimientosID == 2).Select(mm=>mm.Monto).DefaultIfEmpty(0).Sum(),
InteresesPagados = db.movimientos.Where(mm => mm.CreditoID == c.ID && mm.Eliminado != true && mm.DepositoMovimientoID != null && mm.CategoriaMovimientosID == 4).Select(mm => mm.Monto).DefaultIfEmpty(0).Sum(),
SaldoInsoluto = db.movimientos.Where(mm => mm.CreditoID == c.ID && mm.CategoriaMovimientosID == 1).Select(mm => mm.Monto).DefaultIfEmpty(0).Sum() -
db.movimientos.Where(mm => mm.CreditoID == c.ID && mm.Eliminado != true && mm.DepositoMovimientoID != null && mm.CategoriaMovimientosID == 2).Select(mm => mm.Monto).DefaultIfEmpty(0).Sum(),
SaldoDeudorTotal = (db.movimientos.Where(mm => mm.CreditoID == c.ID
&& mm.Eliminado != true
&& mm.DepositoMovimientoID == null
&& mm.CategoriaMovimientosID != 2)
.Select(mm => mm.Monto).DefaultIfEmpty(0).Sum()
- db.movimientos.Where(mm => mm.CreditoID == c.ID
&& mm.Eliminado != true
&& mm.DepositoMovimientoID != null
&& mm.CategoriaMovimientosID != 1)
.Select(mm => mm.Monto).DefaultIfEmpty(0).Sum()),
Migrado = c.Migracion
};
In some of the results of the query, the value of "SaldoDeudorTotal" is negative, I want to control the value, that if the value is -0 then return 0 zero.
var creditos = from c in db.creditos
join s in db.solicitudDelCliente on c.SolicitudDelClienteID equals s.ID
join cl in db.cliente on s.ClienteID equals cl.ID
where c.Eliminado != true && cl.Eliminado != true && c.NegocioID == negocio_id
let total = (db.movimientos.Where(mm => mm.CreditoID == c.ID
&& mm.Eliminado != true
&& mm.DepositoMovimientoID == null
&& mm.CategoriaMovimientosID != 2)
.Select(mm => mm.Monto).DefaultIfEmpty(0).Sum()
- db.movimientos.Where(mm => mm.CreditoID == c.ID
&& mm.Eliminado != true
&& mm.DepositoMovimientoID != null
&& mm.CategoriaMovimientosID != 1)
.Select(mm => mm.Monto).DefaultIfEmpty(0).Sum())
select new
{
c.ID,
cl.NumeroCliente,
c.Consecutivo,
NegocioID = negocio_id,
cl.NombreCompleto,
c.FechaAlta,
c.CapitalPrestado,
c.Vencido,
c.Quebranto,
c.Juridico,
c.Liquidado,
c.Reestructura,
c.Eliminado,
c.Estatus,
CapitalPagado = db.movimientos.Where(mm=>mm.CreditoID == c.ID && mm.Eliminado != true && mm.DepositoMovimientoID != null && mm.CategoriaMovimientosID == 2).Select(mm=>mm.Monto).DefaultIfEmpty(0).Sum(),
InteresesPagados = db.movimientos.Where(mm => mm.CreditoID == c.ID && mm.Eliminado != true && mm.DepositoMovimientoID != null && mm.CategoriaMovimientosID == 4).Select(mm => mm.Monto).DefaultIfEmpty(0).Sum(),
SaldoInsoluto = db.movimientos.Where(mm => mm.CreditoID == c.ID && mm.CategoriaMovimientosID == 1).Select(mm => mm.Monto).DefaultIfEmpty(0).Sum() -
db.movimientos.Where(mm => mm.CreditoID == c.ID && mm.Eliminado != true && mm.DepositoMovimientoID != null && mm.CategoriaMovimientosID == 2).Select(mm => mm.Monto).DefaultIfEmpty(0).Sum(),
SaldoDeudorTotal = total < 0 ? 0 : total,
Migrado = c.Migracion
};
you could use an extension
public static class MyExtension
{
public static int ToPositiveInt(this int value)
{
if (value < 0)
return 0;
else
return value
}
}
and call it at the end of your condition of SaldoDeudorTotal
SaldoDoeudorTotal = (db.movimientos.Where(mm => mm.CreditoID == c.ID
&& mm.Eliminado != true
&& mm.DepositoMovimientoID == null
&& mm.CategoriaMovimientosID != 2).ToPositiveInt()

Entity Framework Core query error when include subquery

I have this EF query
var r = (from cd in _context.CentoData
.Where(c =>
(string.IsNullOrEmpty(geneName) ? true : c.Gene == geneName) &&
(string.IsNullOrEmpty(codingEffect) ? true : c.CodingEffect == codingEffect) &&
(string.IsNullOrEmpty(zygosity) ? true : c.Zygosity == zygosity) &&
(string.IsNullOrEmpty(sex) ? true : c.Sex == sex) &&
(string.IsNullOrEmpty(clinicalStatement) ? true : c.ClinicalStatement == clinicalStatement) &&
(onsetAge == null ? true : c.AgeAtDiagnosisYearsMonths < onsetAge))
from gs in _context.GnomadSnv
.Where(g => g.Chromosome == cd.Chromosome &&
g.Position == cd.Position &&
g.Ref == cd.RefNt &&
g.Alt == cd.AltNt)
.DefaultIfEmpty()
select new resultType
{
name = cd.name,
AF = g.AF
}
And it works well. But when I when I include a Contains subquery as below (line 9), it pops error saying
"The multi-part identifier "c.Chromosome" could not be bound".
patients is a string list with tens to hundreds elements.
var r = (from cd in _context.CentoData
.Where(c =>
(string.IsNullOrEmpty(geneName) ? true : c.Gene == geneName) &&
(string.IsNullOrEmpty(codingEffect) ? true : c.CodingEffect == codingEffect) &&
(string.IsNullOrEmpty(zygosity) ? true : c.Zygosity == zygosity) &&
(string.IsNullOrEmpty(sex) ? true : c.Sex == sex) &&
(string.IsNullOrEmpty(clinicalStatement) ? true : c.ClinicalStatement == clinicalStatement) &&
(onsetAge == null ? true : c.AgeAtDiagnosisYearsMonths < onsetAge) &&
(patients != null ? patients.Contains(c.RandomPatientId): true))
from gs in _context.GnomadSnv
.Where(g => g.Chromosome == cd.Chromosome &&
g.Position == cd.Position &&
g.Ref == cd.RefNt &&
g.Alt == cd.AltNt)
.DefaultIfEmpty()
select new resultType
{
name = cd.name,
AF = g.AF
}
Did I miss something here?

The cast to value type System.Boolean failed because the materialized value is null, result type's generic parameter must use a nullable type

This code was working before but now I've got this error: The cast to value type 'System.Boolean' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
public async Task<ActionResult> BankDepositVoucher(BankDepositVoucherSearchViewModel search, int? PageNo)
{
var model = new BankDepositVoucherListViewModel
{
Search = search ?? new BankDepositVoucherSearchViewModel()
};
if (search != null)
{
search.StartDate = search.StartDate.ToStartOfDay();
search.EndDate = search.EndDate.ToEndOfDay();
}
try
{
var Vouchers = DbManager.Invoices.Include(x => x.BankDepositVoucher)
.Where(x => x.Type == InvoiceType.BankDepositVoucher
&& (x.VoucherNumber == search.VoucherNo || search.VoucherNo == null)
&& (x.BankDepositVoucher.SlipNo.Contains(search.SlipNo) || search.SlipNo == null)
&& (x.BankDepositVoucher.ChequeNo.Contains(search.ChequeNo) || search.ChequeNo == null)
&& (x.BankDepositVoucher.Bank.AccountName.Contains(search.BankDetails)
|| search.BankDetails == null)
&& (x.BankDepositVoucher.AccountName.Contains(search.AccountName) || search.AccountName == null)
&& (x.BankDepositVoucher.Narration.Contains(search.Narration) || search.Narration == null)
&& (x.TotalAmount == search.Amount || search.Amount == null)
&& (x.Date >= search.StartDate || search.StartDate == null)
&& (x.Date <= search.EndDate || search.EndDate == null));
//model.Pager = new Pager(await Vouchers.CountAsync(), PageNo, 10);
model.Vouchers = await Vouchers.OrderByDescending(x => x.VoucherNumber)
//.Skip((model.Pager.CurrentPage - 1) * model.Pager.PageSize)
//.Take(model.Pager.PageSize)
.Select(x => new BankDepositVoucherBaseViewModel
{
Id = x.Id,
VoucherNumber = x.VoucherNumber,
AccountName = x.BankDepositVoucher.AccountName,
BankAccountName = x.BankDepositVoucher.Bank.AccountName,
Date = x.Date,
ChequeNo = x.BankDepositVoucher.ChequeNo,
Narration = x.BankDepositVoucher.Narration,
SlipNo = x.BankDepositVoucher.SlipNo,
TotalAmount = x.TotalAmount,
IsCleared = x.BankDepositVoucher.IsCleared
}).ToListAsync();
}
catch (Exception ex)
{
Console.WriteLine("", ex.Message);
}
return PartialView(model);
}
This is the part throwing above mentioned exception
model.Vouchers = await Vouchers.OrderByDescending(x => x.VoucherNumber)
//.Skip((model.Pager.CurrentPage - 1) * model.Pager.PageSize)
//.Take(model.Pager.PageSize)
.Select(x => new BankDepositVoucherBaseViewModel
{
Id = x.Id,
VoucherNumber = x.VoucherNumber,
AccountName = x.BankDepositVoucher.AccountName,
BankAccountName = x.BankDepositVoucher.Bank.AccountName,
Date = x.Date,
ChequeNo = x.BankDepositVoucher.ChequeNo,
Narration = x.BankDepositVoucher.Narration,
SlipNo = x.BankDepositVoucher.SlipNo,
TotalAmount = x.TotalAmount,
IsCleared = x.BankDepositVoucher.IsCleared
}).ToListAsync();
The issue is likely that when populating the view model it cannot deal with the fact that a record may not have a BankDepositVoucher.
For instance:
IsCleared = x.BankDepositVoucher.IsCleared
This should probably be:
IsCleared = x.BankDepositVoucher?.IsCleared ?? false
One other thing to improve performance considerably:
While it may look concise in the code to write statements like this:
.Where(x => x.Type == InvoiceType.BankDepositVoucher
&& (x.VoucherNumber == search.VoucherNo || search.VoucherNo == null)
&& (x.BankDepositVoucher.SlipNo.Contains(search.SlipNo) || search.SlipNo == null)
&& (x.BankDepositVoucher.ChequeNo.Contains(search.ChequeNo) || search.ChequeNo == null)
&& (x.BankDepositVoucher.Bank.AccountName.Contains(search.BankDetails)
|| search.BankDetails == null)
&& (x.BankDepositVoucher.AccountName.Contains(search.AccountName) || search.AccountName == null)
&& (x.BankDepositVoucher.Narration.Contains(search.Narration) || search.Narration == null)
&& (x.TotalAmount == search.Amount || search.Amount == null)
&& (x.Date >= search.StartDate || search.StartDate == null)
&& (x.Date <= search.EndDate || search.EndDate == null));
It is more efficient to write it out as:
.Where(x => x.Type == InvoiceType.BankDepositVoucher);
if(!string.IsNullOrEmpty(search.VoucherNo))
Voucher = Voucher.Where(x => x.VoucherNumber == search.VoucherNo);
if(!string.IsNullOrEmpty(search.SlipNo))
Voucher = Voucher.Where(x => x.BankDepositVoucher.SlipNo.Contains(search.SlipNo))
// etc.
The reason is that in the first case you are generating a much larger SQL statement to be sent to the database, and it is quite easy to "slip up" on conditions if that query is ever edited in the future. (missing parenthesis, etc.) The second example only adds conditions to the query if they are needed, keeping the resulting SQL statement much more compact.

C# use left outer join in LINQ

This is my method:
public IEnumerable<Web_Vendor> GetSaleBrands()
{
using (var _db = new LuxedecorContext())
{
IQueryable<Web_Vendor> web_vendors = _db.Web_Vendor.
Where(x =>
(_db.Web_Promotion.Where(z => z.VendorID != string.Empty
&& z.Static == true && z.Percent != 0 &&
(x.WebPromotion.Expires >= DateTime.Now || x.WebPromotion.Expires == null))
.Select(r => r.VendorID).Contains(x.VendorID))
||
(_db.NavItems.Where(y => x.WebPromotion.VendorID == y.SC1
&& !(y.Promotion == "" || y.Promotion == null)
&& (y.PromotionStart <= DateTime.Now) && (y.PromotionEnd >= DateTime.Now || y.PromotionEnd == null))
.Select(g => g.SC1).Contains(x.WebPromotion.VendorID)))
.Where(x => x.Active == true).OrderBy(x => x.NameExtra)
.ThenBy(x => x.Sequence);
var a = web_vendors.ToList(); // 16 values
var b = web_vendors.Include(x => x.WebPromotion).ToList(); // 13 values
}
throw new NotImplementedException();
}
I need to Include nullable values to my collection of Web_Vendors. I know, it's famous question, but I need to return collection of Web_Vendors instead of collection of dynamic types. Is it possible?

Why is my query not returning anything

I was writing a LINQ query to filter the records based on user input and selection. Some of the inputs may not be given from the user. So i need to filter based on the given input. I tried giving value for only 1 out of 5 optional inputs. But the query is not returning anything. Please help me to find the proper query. you can better understand after seeing the query.
Code
var model = (from items in Db.Items
where ((items.ItemNo == null ||
items.ItemNo == String.Empty) ||
((items.ItemNo.CompareTo(DD.FromItemNo) >= 0) &&
(items.ItemNo.CompareTo(DD.ToItemNo) <= 0))) &&
(items.InfoTypeId == 0 ||
(items.InfoTypeId == DD.InfoType)) &&
(items.CreatedOn == null ||
(items.CreatedOn >= DD.Start &&
items.CreatedOn <= DD.End)) &&
(items.StatusId == 0 ||
(items.StatusId == DD.Status)) &&
(items.LocationId == 0 ||
(items.LocationId == DD.Location)) &&
(items.CollectionId == 0 ||
(items.CollectionId == DD.Collection))
select new ViewModel()
{
Itemid = items.Id,
INo = items.ItemNo,
BTags = (from asd in Db.BibContents
where asd.BibId == items.BibId &&
asd.TagNo == "245" &&
asd.Sfld == "a"
select asd.Value).FirstOrDefault(),
Sid = (from stat in Db.ItemStatus1
where stat.Id == items.StatusId
select stat.Description).FirstOrDefault(),
Option = DD.Option,
CurrItemNo = DD.ItemNumber
}).ToList();
You've got to check the values of DD for nulls or 0s, not those of items:
var model = (from items in Db.Items
where
(
(DD.ItemNo == null || DD.ItemNo == String.Empty)
|| (items.ItemNo.CompareTo(DD.FromItemNo) >= 0 && items.ItemNo.CompareTo(DD.ToItemNo) <= 0)
)
&& (DD.InfoTypeId == 0 || (items.InfoTypeId == DD.InfoType))
&& (DD.CreatedOn == null || (items.CreatedOn >= DD.Start && items.CreatedOn <= DD.End))
&& (DD.StatusId == 0 || (items.StatusId == DD.Status))
&& (DD.LocationId == 0 || (items.LocationId == DD.Location))
&& (DD.CollectionId == 0 || (items.CollectionId == DD.Collection))
select ...

Categories

Resources