English 中文(简体)
C# Convert. ToDouble(价值) in Lambda LINQ 声明
原标题:C# Convert.ToDouble(value) in Lambda LINQ Statement
  • 时间:2011-08-30 15:15:38
  •  标签:
  • c#
  • linq

I have the following LINQ statement:

Items = Items.Where(p => p.LeadDatas.Any(
                         q =>
                         q.LeadField.Name == descriptor.Name &&
                         Convert.ToDouble(q.Value) == Convert.ToDouble(value)));

<代码>q.Value是两倍的累进价值, Value><<>> >也是两倍的累进价值,这两者都需转换成两倍,以便比较平等。

在我对《准则》发言进行反驳时,我收到以下意见:

垂直数据类型变数

我不敢肯定为什么不允许我这样做,但我不知道这套办法是什么,我需要把我的两个价值观进行比较,以便在这里实现平等。

最佳回答

首先,我要从<代码>Convert.ToDouble(价值)中抽出一个地方变量:

double target = Convert.ToDouble(value);
Items = Items.Where(p => p.LeadDatas.Any(q =>
                             q.LeadField.Name == descriptor.Name &&
                             Convert.ToDouble(q.Value) == target));

很可能把这一问题推向“ /code”的转换尝试,而不是试图转换q。 某些行文的数值。 或者说,可以是,你走了一条没有有效价值的牢牢。 (请try > 查询double。 TryParse ,但Im 不能确定这种工作十分出色......

然而,将双向浮动点值与简单平等相比较,这种概念通常是错误的。 你们可能希望使用一定程度的宽容(确切地说,这如何与LINQ至QK合作是另一个问题......)

问题回答

Check to see that the string values you are converting to float in SQL Server are valid (e.g., they aren t blank or they have a valid symbol for decimal point).

如同一种价值一样,它无法成功地转化为浮动。

它希望你重新使用LINQ到Q。 这一错误直接来自执行询问的服务器(不是你的代码)。 我的猜测是,你拥有价值不算有效数字的SOME。

我将在SSMS中处理以下问题,我认为你发现这错了。

Error Converting data type varchar to float

select convert(float, value) from leaddata

EDIT:

如果你想像约恩所建议的那样增加一些错误的容忍,那么你可以规划IsNumeric的功能,并做以下工作:

参看 如何知道在Linq To的一个领域是数字的

<Function Name="ISNUMERIC" IsComposable="true">
    <Parameter Name="Expression" Parameter="Expression" Type="System.String" DbType="NVarChar(4000)" />
    <Return Type="System.Boolean" DbType="BIT NOT NULL"/>
</Function>

在您的法典中:

double target = Convert.ToDouble(value);
Items = Items.Where(p => p.LeadDatas.Where(i => myDataContext.IsNumeric(i)).Any(q =>
                             q.LeadField.Name == descriptor.Name &&
                             Convert.ToDouble(q.Value) == target));




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...