How to use denial in Contains c# with LINQ?

Asked

Viewed 217 times

3

I need help, I’m starting in development, eclistIds returns me two ids, and if I don’t use the negation ( ! ) it returns me the id I don’t want, when I use the negation to return the other id, according to the leftjoin, error occurs

My ids are decimal

List<ExtraModel> add= (from fpp in ADD
join fpc in FPC                                                                          
   on new { fpp.fpId, fpp.etId }                                                                             
   equals new { fpc.fpId, fpc.etId }
join ec in EP on fpc.ecId equals ec.ecId
join fpcp in FPCP                                                                            
   on new { fpp.fppId, fpc.ecId }                                                                            
   equals new { fpcp.fppId, fpcp.ecId } into x
from d in x.DefaultIfEmpty()
where fpp.fpId == FPId                                                                           
      && fplistIds.Contains(fpp.fpId)                                                                            
      && etlistIds.Contains(fpp.etId)                                                                
      && !eclistIds.Contains(d.ecId)
select new ExtraModel {
fppId = d.fppId,
ecId = d.ecId }).ToList();

The error that appears is as follows:

The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

SOLUTION!

var fpcpAdd = (from fpp in FPP
join fpc in FPC 
    on new { fpp.fpId, fpp.etId } 
    equals new { fpc.fpId, fpc.etId }
join fpcp in FPCP 
    on new { fpp.fppId, fpc.ecId } 
    equals new { fpcp.fppId, fpcp.ecId } into x
from d in x.DefaultIfEmpty()
where fpp.fpId == fpId 
    && fpIds.Contains(fpp.fpId)
    && etIds.Contains(fpp.etId)
    && ecIds.Contains(fpc.ecId)
    && fpp.fppId != d.fppId
select new {
    fpp.fppId,
    fpc.ecId }).ToList();

THANK YOU ALL!

  • 1

    This problem, I believe, is not no contains, but conversion.

  • According to the error, the problem is in converting a null value to Decimal.

  • You will have decimal values to null in the query return you should put in your Extramodel class the fields fppId and ecid as Decimal?

  • Perfect, thanks, but how do I fix it?

  • Ricardo I can’t change the extraModel because it’s a big system and it causes several errors .... The solution would have to be in Brasili

1 answer

3

Make a method that converts the value and if it is null, return 0 or -1, for example.

public static decimal TratarDecimal(object valor)
{
    decimal result;
    if(decimal.TryParse(Convert.ToString(valor), out result))
    {
        return result;
    }
    else
    {
        return 0;
    }
}

In the Linq check if this will work, assuming the method is within a class called Helper.

List<ExtraModel> add= (from fpp in ADD
join fpc in FPC                                                                          
   on new { fpp.fpId, fpp.etId }                                                                             
   equals new { fpc.fpId, fpc.etId }
join ec in EP on fpc.ecId equals ec.ecId
join fpcp in FPCP                                                                            
   on new { fpp.fppId, fpc.ecId }                                                                            
   equals new { fpcp.fppId, fpcp.ecId } into x
from d in x.DefaultIfEmpty()
where fpp.fpId == FPId                                                                           
      && fplistIds.Contains(fpp.fpId)                                                                            
      && etlistIds.Contains(fpp.etId)                                                                
      && !eclistIds.Contains(d.ecId)
select new ExtraModel {
fppId = Helper.TratarDecimal(d.fppId),
ecId = Helper.TratarDecimal(d.ecId) }).ToList();

Maybe the ToList() needs to be called before the select, if error occurs let me know that I make an adjustment here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.