English 中文(简体)
LINQ 选择问题总是导致实际价值
原标题:LINQ select query always resulting in true value
  • 时间:2012-05-10 15:04:48
  •  标签:
  • c#
  • linq

当我打下以下一线时,我总是收到真实情况,不管“当地”是否出现在加入<编码>的的表格中。

if (objUserRoles.Select(x => (x.Role.Role1 == "local")).Count() > 0)

我的同仁是否正确?

最佳回答

What you need is Where:

if (objUserRoles.Where(x => x.Role.Role1 == "local").Count() > 0)

或者有<条码>Any,它更有利(而且表现良好,因为在大多数情况下,它赢得了像<条码>Count(<>)这样的全收。

 if (objUserRoles.Any(x => x.Role.Role1 == "local"))
问题回答

请回顾:

if (objUserRoles.Any(x => x.Role.Role1 == "local"))

您在选择一系列<代码>bool。 E.g. 如果您的问询有3项内容,即:代码>false、真实、虚假的,那么你要求的是<代码>false、真实、虚假的<>/code序列,而不仅仅是在真实地点标的。 它希望你重新尝试选择这一系列保ool的真实价值,这意味着你应当使用<代码>。 在而不是Select的情况下。 然而,由于你们都重新采用了这一条码。 如果和对至少其中一项进行核对,请填写。 任何,其效率更高。

我认为,你想要使用......Where(expr)而不是“Select(expr)。

最好有<条码>。 任何

if (objUserRoles.Where(x => (x.Role.Role1 == "local")).Any())

正如其他人所指出的,Any()或.Where(Count())将给你重新寻找的东西。 页: 1

if (objUserRoles.Select(x => (x.Role.Role1 == "local")).Count() > 0)

......实际上正在创建<代码>。 IE amountable<bool>,其中每个项目都是根据x进行的。 作用:Role1=“当地”。 您的最终结果总是真实的,因为除非你的切除,否则>.Count(>>>>s>? 用户名册实际上包含0个元素。

希望澄清对你们来说是有意义的!

您的发言也没有将<条码>objUserRoles至<条码>(boolean)中的任何内容转换成“条码”。

objUserRoles.Select(x => (x.Role.Role1 == "local"))

is a IEnumerable<bool> with as many elements of your objUserRoles collection. i guess what you want to do is described by the following query

if (objUserRoles.Where(x => x.Role.Role1 == "local").Count() > 0)

甚至更好

if (objUserRoles.Any(x => x.Role.Role1 == "local"))




相关问题
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. ...

热门标签