开发者

LINQ query, ignoring results with certain decimal points

开发者 https://www.devze.com 2023-04-01 04:02 出处:网络
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 dat

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;


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号