I have a class as:
public class PersianDate
{
public int Year;
public int Month;
public int Day;
public int Hour;
public int Minute;
public int Second;
public string MonthName;
}
I want that if I convert it like here:
HTools.PersianDate pDate=new HTools.PersianDate();
string date = pDate.ToString();
And I want date to be:
1396-06-14T19:17:38
How can I do that?
public class PersianDate
{
public int Year;
public int Month;
public int Day;
public int Hour;
public int Minute;
public int Second;
public string MonthName;
public override string ToString()
{
return string.Format("{0}-{1}-{2}T{3}:{4}:{5}",Year,Month,Day,Hour,Minute,Second);
}
}
Override ToString() method from object class to get the format you want.
DotNetFiddle Example.
If you want a string that represent the object as a json you can use the "Newtonsift.Json" Nuget package:
PersianDate thing = new PersianDate();
//TODO: fill you thing with the data you need
string json = JsonConvert.SerializeObject(thing);
If you want specific string - ovverride the ToString methods in your class:
public override string ToString()
{
return $"{Year}-{Month}-{Day}T{Hour}:{Minute}:{Second}";
}
Related
I have a following JSON response:
{
"data": [
{
"ac_conditions": "[{\"ac_condition_group_id\":156570,\"ac_condition_group_name\":\"\u0413\u0440\u0443\u043f\u043f\u0430 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 1\",\"id\":311790,\"ac_parameter\":\"utm_source\",\"ac_operator\":\"=\",\"value\":\"google_rem\",\"is_negative\":false},{\"ac_condition_group_id\":156570,\"ac_condition_group_name\":\"\u0413\u0440\u0443\u043f\u043f\u0430 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 1\",\"id\":275094,\"ac_parameter\":\"utm_source\",\"ac_operator\":\"=\",\"value\":\"yandex_retargeting\",\"is_negative\":false}]",
"is_dt_enabled": true,
"ac_id": 162866,
"site_blocks": "[{\"id\":324164,\"name\":\"\u041d\u043e\u043c\u0435\u0440 1 \u043d\u0430 \u0441\u0430\u0439\u0442\u0435\",\"phone_type\":\"virtual\",\"numb\":\"74950238629\",\"forward_numb\":null,\"is_dt_enabled\":true,\"dt_number_pool_numbers\":[\"74951828912\",\"74950324045\",\"74950324046\",\"74950324043\",\"74950324037\",\"74951828907\",\"74950324048\",\"74950324049\",\"74951523589\",\"74953239984\"]}]"
}
],
"success": true
}
I create a class to deserealize it
public class Condition
{
public bool success;
public List<Data> data;
public class Data
{
public string ac_conditions;
public int ac_id;
public bool is_dt_enabled;
public string site_blocks;
};
}
It's working fine. What I need is to deserealize also the elements ac_conditions and site_blocks. I have created a new class but I get an exception (System.String cannot cast to List)
public class Condition
{
public bool success;
public List<Data> data;
public class Data
{
public List<ConditionCamp> ac_conditions;
public int ac_id;
public bool is_dt_enabled;
public List<SiteBlock> site_blocks;
public class ConditionCamp
{
public int ac_condition_group_id;
public string ac_condition_group_name;
public int id;
public string ac_parameter;
public string ac_operator;
public string value;
public bool is_negative;
}
public class SiteBlock
{
public int id;
public string name;
public string phone_type;
public string numb;
public string forward_numb;
public bool is_dt_enabled;
public string dt_number_pool_numbers;
}
};
}
I use this line in my code to deserialize the JSON response
JsonConvert.DeserializeObject<Models.Condition>(string_Condition);
Your JSON property "site_blocks" is a string value containing serialized JSON-data. Therefore you need a second step to unwrap/deserialize the data. If you can change the the way how the response is generated you can fix it there (return JSON in site_blocks and no string)
E.g. (using Json.net and results of second parse run are store in site_blocks_parsed)
public class Condition
{
public bool success;
public List<Data> data;
public class Data
{
public string ac_conditions;
public int ac_id;
public bool is_dt_enabled;
public string site_blocks;
public List<SiteBlock> site_blocks_parsed;
public class ConditionCamp
{
public int ac_condition_group_id;
public string ac_condition_group_name;
public int id;
public string ac_parameter;
public string ac_operator;
public string value;
public bool is_negative;
}
public class SiteBlock
{
public int id;
public string name;
public string phone_type;
public string numb;
public string forward_numb;
public bool is_dt_enabled;
public string dt_number_pool_numbers;
}
};
}
...
var condition = JsonConvert.DeserializeObject<Condition>(jsonString);
foreach (var data in condition.data) {
data.site_blocks_parsed = JsonConvert.DeserializeObject<List<SiteBlock>>(data.site_blocks);
}
I duplicated a Class1 in a Class2 with the same properties but not all ones. In my context, I have a Class1 obj (o1) initialised with values and I want to change some ones with values of a Class2 obj (o2) without losing the o1 data (values which aren't in o2). Manually, I need to reassignate o1 with values of o2 so I would like to find an automatic way like mapping the classes.
Classes:
public class Class1
{
public bool Property1;
public int Property2;
public string Property3;
public bool Property4;
public int Property5;
public string Property6;
public bool Property7;
public int Property8;
public string Property9;
public bool Property10;
public int Property11;
public string Property12;
public bool Property13;
public int Property14;
public string Property15;
public bool Property16;
public int Property17;
public string Property18;
public bool Property19;
public int Property20;
}
public class Class2
{
public bool Property1;
public int Property2;
public string Property3;
public bool Property7;
public int Property8;
public string Property9;
public bool Property10;
public int Property11;
public string Property12;
public bool Property13;
public int Property14;
public string Property15;
public bool Property16;
public int Property17;
public string Property18;
public bool Property19;
public int Property20;
public Class1 ToClass1()
{
return new Class1()
{
Property1 = Property1,
Property2 = Property2,
Property3 = Property3,
Property7 = Property7,
...
Property20 = Property20
};
}
}
If I do ToClass1(), I lose the values of Property4, Property5 and Property6 of the o1.
What I do now:
// o1 and o2 are initialised with values.
o1.Property1 = o2.Property1;
o1.Property2 = o2.Property2;
o1.Property3 = o2.Property3;
o1.Property7 = o2.Property7;
...
o1.Property20 = o2.Property20;
you need to use automapper and configure your mapping rule to ignore the properties you wont want to map.
Look at this link
Is there a type like enum that would allow me to merge these variable into one
private string StringPropertie;
private int IntPropertie;
private float floatPropertie;
private DateTime DatetimePropertie;
private bool boolPropertie;
to something has follow.
private enumtype property
You can use structure
public struct MyStruct
{
public string StringPropertie;
public int IntPropertie;
public float floatPropertie;
public DateTime DatetimePropertie;
public bool boolPropertie;
}
public class MyClass
{
public MyClass()
{
MyStruct property ;
//...
string str = property.StringPropertie;
}
}
I am using FileHelpers to create NACHA files. Example below
Many of the properties are numeric with leading zeros so they have been defined as strings. Is there an attribute that can pad the leading zeros in the Class property, similar to the way FieldTrim/TrimMode works to remove whitespace?
[FixedLengthRecord()]
public class FileControlRecord
{
[FieldFixedLength(1)]
public int RecordTypeCode = 9; //Constant
[FieldFixedLength(6)]
public string BatchCount; //Numeric
[FieldFixedLength(6)]
public string BlockCount; //Numeric
[FieldFixedLength(8)]
public string EntryAddendaCount; //Numeric
[FieldFixedLength(10)]
public string EntryHash; //Numeric
[FieldFixedLength(12)]
public string TotalDebit; //$$$$$$$$$$cc
[FieldFixedLength(12)]
public string TotalCredit; //$$$$$$$$$$cc
[FieldFixedLength(39)]
[FieldNotInFile]
public string RESERVED; //Blank
}
You must use FieldAlign with the padding char:
[FixedLengthRecord()]
public class FileControlRecord
{
[FieldFixedLength(1)]
public int RecordTypeCode = 9; //Constant
[FieldFixedLength(6)]
public string BatchCount; //Numeric
[FieldFixedLength(6)]
public string BlockCount; //Numeric
[FieldFixedLength(8)]
public string EntryAddendaCount; //Numeric
[FieldFixedLength(10)]
public string EntryHash; //Numeric
[FieldFixedLength(12)]
[FieldAlign(AlignMode.Right, '$')]
public string TotalDebit; //$$$$$$$$$$cc
[FieldFixedLength(12)]
[FieldAlign(AlignMode.Right, '$')]
public string TotalCredit; //$$$$$$$$$$cc
[FieldFixedLength(39)]
public string RESERVED; //Blank
}
PS: I recomend you to use the last version of the library:
https://github.com/MarcosMeli/FileHelpers/releases/latest
or via NuGet https://www.nuget.org/packages/FileHelpers/
I had a similar question answered earlier, then my boss told me to sort it based on date so here I am again.
I have this -
List<User> users;
protected class User : IComparable<User>
{
public string name;
public string email;
public decimal total;
public string address;
public string company;
public string placed;
public string fulfilled;
public string origin;
public int CompareTo(User b)
{
// Alphabetic sort name[A to Z]
return this.placed.CompareTo(b.placed);
}
}
My datetime format is MM/DD/YYYY, so it goes by the month, not by the whole thing. What's the best way to get it to sort based on the full date? Thanks!
You know it is a date, then why store it as string? use dedicated DateTime type. It will work as expected.
If you can't change the field to DateTime for some reason, you can always do the following
public int CompareTo(User b)
{
DateTime x = DateTime.ParseExact(this.placed,...);
DateTime y = DateTime.ParseExact(b.placed,...);
return x.CompareTo(y);
}