Search code examples
c#linqdoublewhere-clausedecimal-point

LINQ query, ignoring results with certain decimal points


I need to perform a LINQ query on a large database in C#. One of the columns I need to use in the query is a double. I need to omit results that have more than 4 decimal places in this column. The database can't be changed as other programs need to use it and make use of what I don't want. The results are then added to a list to use later. I thought that this would work.

where fun.Units != '*.?????*'

However it returns the error that too many characters are in the character literal. The whole query looks like this so far

var clientQuery1 = from cli in main1.Clients
                   from pol in main1.Policies
                   from fun in main1.FundHoldings
                   from uni in main1.UnitPrices
                   where cli.AccountNumber == accNum
                   && pol.ClientRef == cli.ClientRef
                   && fun.FKeyRef == pol.PolicyRef
                   && uni.UnitPriceRef == fun.UnitPriceRef
                   && fun.Units != '*.?????*'
                   select uni.UnitName;

Solution

  • Can you please try with this below query and let me know.

    var clientQuery1 = from cli in main1.Clients
                       from pol in main1.Policies
                       from fun in main1.FundHoldings
                       from uni in main1.UnitPrices
                       where cli.AccountNumber == accNum
                       && pol.ClientRef == cli.ClientRef
                       && fun.FKeyRef == pol.PolicyRef
                       && uni.UnitPriceRef == fun.UnitPriceRef
                       && fun.Units == Math.Round(Convert.ToDouble(fun.Units),4)
                       select uni.UnitName;