nullable

Linq query with nullable sum

你说的曾经没有我的故事 提交于 2019-12-27 23:36:26
问题 from i in Db.Items select new VotedItem { ItemId = i.ItemId, Points = (from v in Db.Votes where b.ItemId == v.ItemId select v.Points).Sum() } I got this query, however it fails if no votes are found with exception: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type. I assume its because sum returns an int and not a nullable int, giving sum a int? as input only give the same error, probably cause sum only workes on ints. Any good workaround

Linq query with nullable sum

非 Y 不嫁゛ 提交于 2019-12-27 23:36:12
问题 from i in Db.Items select new VotedItem { ItemId = i.ItemId, Points = (from v in Db.Votes where b.ItemId == v.ItemId select v.Points).Sum() } I got this query, however it fails if no votes are found with exception: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type. I assume its because sum returns an int and not a nullable int, giving sum a int? as input only give the same error, probably cause sum only workes on ints. Any good workaround

Which is preferred: Nullable<T>.HasValue or Nullable<T> != null?

吃可爱长大的小学妹 提交于 2019-12-27 09:52:08
问题 I always used Nullable<>.HasValue because I liked the semantics. However, recently I was working on someone else's existing codebase where they used Nullable<> != null exclusively instead. Is there a reason to use one over the other, or is it purely preference? int? a; if (a.HasValue) // ... vs. int? b; if (b != null) // ... 回答1: The compiler replaces null comparisons with a call to HasValue , so there is no real difference. Just do whichever is more readable/makes more sense to you and your

Can you add additional properties to .NET serialized NULL values?

杀马特。学长 韩版系。学妹 提交于 2019-12-25 12:55:48
问题 In .NET, you can serialize a nullable element using XmlElement( IsNullable = true ), which results in this: <SomeElement xsi:nil="true" />. However, I need to be able to add xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance after any field that is being nulled out. Example: <SomeElement xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> Is this possible? 回答1: I figured it out. Here is the answer: [XmlElement(IsNullable = true, Namespace = "http://www.w3.org/2001

How can I pad nullable ints with zeros?

流过昼夜 提交于 2019-12-25 06:56:04
问题 Is there any way to pad nullable int with zeros the way you can with normal int? the myInt.ToString("D3") doesn't seem to work on nullale ints but for my current project I feel like I need to use nullable int in order to get the null value in my array instead of 0 as the default. 回答1: That is because int? is actually Nullable<T> where T is int here. Calling ToString will call that on the Nullable struct, not the int . Nullable<T> has no knowledge of the overloads of ToString which int has.

How to handle nullable integer columns in SQLite database

断了今生、忘了曾经 提交于 2019-12-24 19:29:27
问题 I have an Android SQLite table definition like this: create table MyTable(..., myDate integer, ...); Later in my code I query this table to retrieve the value for myDate via a cursor: long dateInstant = cursor.getLong(cursor.getColumnIndexOrThrow("myDate")); However, I'm wondering if this is the best approach. If the value is not set (i.e. null on the database) the above method returns 0, which happens to also be a valid value. I need to differentiate valid values from null values. Also, from

How to serialize nullable DateTime in a WCF DataContract?

大憨熊 提交于 2019-12-24 12:42:06
问题 I am having trouble serializing this thing. namespace Such.Namespace.Wow { [DataContract(Namespace = "http://very.namespace.uh")] public class DogeResponse { [DataMember] public virtual int Foo { get; set; } [DataMember] public virtual DateTime? ExpiringDate { get; set; } } } In generated WSDL : <xs:complexType name="DogeResponse"> <xs:sequence> <xs:element minOccurs="0" name="Foo" type="xs:int"/> <xs:element minOccurs="0" name="ExpiringDate" nillable="true" type="xs:dateTime"/> </xs:sequence

XML deserialize null elements?

前提是你 提交于 2019-12-24 09:57:16
问题 trying to deserialize a Xml string, but always get problem for elements like these: <Taxable /> <DefaultPurchasePrice /> My C# code snippet: [XmlRoot(ElementName = "Product", Namespace = "http://api.test.com/version/1", IsNullable = false)] public class Product { public Guid Guid { get; set; } public string ProductName { get; set; } public bool Taxable { get; set; } public Decimal DefautSellPrice { get; set; } [XmlElement("DefaultPurchasePrice")] public string DefaultPurchasePriceElement {

“Specified cast is not valid” error in LINQ's orderby clause

流过昼夜 提交于 2019-12-24 02:34:02
问题 I am doing a right join between two datatables in LINQ. I am getting an error at the orderby line ""Specified cast is not valid". "SomeOtherID" column is of type System.Int64 in the dbml and allows DBNull. The column has some null values in the data, which is valid. It seems these nulls have to be handled in a certain way in the orderby statement but I am not sure how. The data is coming in through a web service. I checked the reference.cs file and the corresponding property for the column is

C# Entity Framework select max after where filter of not nullable field

北慕城南 提交于 2019-12-24 00:47:31
问题 I have a not nullable field ( Num ) class MyTable { //... public int Num { get; set; } public string Category { get; set; } //... } want to find maximum Num for Category == "A" var maxnum = myTable .Where(r => r.Category == "A") .Max(r => r.Num); the problem occurred when there wasn't any record of category == "A" . Because the result of Where() is null so the result of Max() will be null but when Num is not nullable the exception occurred. I can fix it by setting Num as nullable in table