Question

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;
Was it helpful?

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;

OTHER TIPS

Well you can solve that particular error using:

&& fun.Units != "*.?????*"

Note the change from single quotes to double quotes. However, that's not going to help you overall. What's the type of fun.Units in LINQ? If it's decimal, you might be able to use:

&& decimal.Round(fun.Units, 4) == fun.Units

... but it's not clear to me what that will do in the generated SQL. It's worth a try, but even if it works you should see what the SQL looks like.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top