草庐IT

group_value

全部标签

c# - 打开可空 bool 值 : case goes to null when value is true

我意识到处理可空类型的正确方法是使用HasValue属性。但我想知道为什么以下switch语句会在null情况下而不是默认情况下中断。使用VS2015C#4.0。另一台使用VS2010C#4.0的计算机没有同样的问题。privatevoidTesting(){bool?boolValue=true;switch(boolValue){casenull:break;//eventhoughvalueistrue,coderunsheredefault:break;}}编辑:观察到任何Nullable的行为如果只有caseNull和default已指定。 最佳答

c# - IEnumerable Group 按用户指定的动态键列表

我有一个类publicclassEmpolyee{publicstringDesignation{get;set;}publicstringDiscipline{get;set;}publicintScale{get;set;}publicDateTimeDOB{get;set;}publicintSales{get;set;}}并以可枚举的方式记录所有员工ListEmployees;和一个字符串键列表,例如varKeys=newList(){"Designation","Scale","DOB"};假设列表“键”的元素是用户指定的,用户可以不指定或指定多个键元素。现在我想使用列表“K

c# - 如何在 Linq to SQL 中使用 distinct 和 group by

我正在尝试将以下sql转换为Linq2SQL:selectgroupId,count(distinct(userId))fromprocessroundissueinstancegroupbygroupId这是我的代码:varq=fromiinProcessRoundIssueInstancegroupibyi.GroupIDintogselectnew{Key=g.Key,Count=g.Select(x=>x.UserID).Distinct().Count()};当我运行代码时,我不断收到无效的GroupID。有任何想法吗?似乎distinct把事情搞砸了..这里是生成的sql:

c# - CodeContracts : Boolean condition evaluates to a constant value, 为什么?

我收到此警告但无法找出问题...CodeContracts:warning:TheBooleanconditiond1.Count!=d2.Countalwaysevaluatestoaconstantvalue.Ifit(oritsnegation)appearinthesourcecode,youmayhavesomedeadcodeorredundantcheck代码如下:publicstaticboolDictionaryEquals(IDictionaryd1,IDictionaryd2){if(d1==d2)returntrue;if(d1==null||d2==null)

c# - VB.NET linq group by 匿名类型不能按预期工作

我正在研究LINQPad附带的一些linq示例。在“C#3.0inaNutshell”文件夹中的Chater9-Grouping下,有一个名为“GroupingbyMultipleKeys”的示例查询。它包含以下查询:fromninnew[]{"Tom","Dick","Harry","Mary","Jay"}.AsQueryable()groupnbynew{FirstLetter=n[0],Length=n.Length}我将字符串“Jon”添加到数组的末尾以获得实际分组,并得出以下结果:这正是我所期待的。然后,在LINQPad中,我转到同一查询的VB.NET版本:'Manuall

c# - 在 Linq-To-Sql 中避免 "Nullable object must have a value."

我有一个这样的方法查询:publicIListGetBusinessObject(Guid?filterId){using(vardb=newL2SDataContext()){varresult=fromboindb.BusinessObjectswhere(filterId.HasValue)?bo.Filter==filterId.value:trueorderbybo.NameselectSqlModelConverters.ConvertBusinessObject(bo);returnresult.ToList();}}在运行时,这会抛出一个System.InvalidOp

c# - GetMonthName : Valid values are between 1 and 13, 包括在内。为什么?

我不小心将0传递给DateTimeFormatInfo的GetMonthName方法:DateTimeFormatInfoinfo=newDateTimeFormatInfo();varmonthName=info.GetMonthName(0);并得到一个System.ArgumentOutOfRangeException错误消息:有效值在1到13之间,包括在内。传入1到12将返回“January”到“December”,但传入13将返回一个空字符串。我明白为什么月份数字不是零索引的,但是第13个月是做什么用的? 最佳答案 这是因

c# - 将 DBNull.Value 和空文本框值传递给数据库

这个问题在这里已经有了答案:HowdoIParameterizeanullstringwithDBNull.Valueclearlyandquickly(8个答案)关闭8年前。我的页面上有一些文本框可以为空,因为它们是可选的,而且我有这个DAL代码parameters.Add(newSqlParameter("@FirstName",FirstName));parameters.Add(newSqlParameter("@LastName",LastName));parameters.Add(newSqlParameter("@DisplayName",DisplayName));pa

c# - 流利的断言 : Assert one OR another value

使用流畅的断言,我想断言给定的字符串包含两个字符串之一:actual.Should().Contain("oneWay").Or().Should().Contain("anotherWay");//eitervalueshouldpasstheassertion.//forexample:"youmaydoitoneWay."shouldpass,but//"youmaydoitthisWay."shouldnotpass只有当两个值都不包含时,断言才会失败。这不起作用(甚至无法编译),因为没有Or()运算符。我现在是这样做的:boolisVariant1=actual.Contai

c# - 通过显式本地化获取资源值(value)

对于不同的资源文件(*.resx),如何通过显式本地化来检索本地化值。也就是说,通常我可以直接引用带有custom-tool-namespace.Resource.localizedAttribute的属性。它将提供的值取决于为CurrentCulture设置的本地化(线程方式)。但与此不同的是,我想将本地化交给资源getter。这可能吗? 最佳答案 假设你有多个资源文件:Messages.resxMessages.fr-FR.resx...Messages.xx-XX.resx都包含一些字符串值,您可以检索特定文化的值:varcu