Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
Use an extension method to convert from string to enum.
public static T ToEnum(this string enumString)
{
return (T) Enum.Parse(typeof (T), enumString);
}
//Usage:
Color colorEnum = "Red".ToEnum<Color>();
Encoding
byte[] textAsBytes = System.Text.Encoding.UTF8.GetBytes(text);
var encodedText = Convert.ToBase64String(plainTextBytes);
Decoding
var textAsBytes = Convert.FromBase64String(text);
var decodedText = System.Text.Encoding.UTF8.GetString(textAsBytes);
How to get the raw xml from a webservice request or response
XmlSerializer xmlSerializer = new XmlSerializer(oResponse.GetType());
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, oResponse);
var xmlText = textWriter.ToString();
}
Fast test way to sum from 1 to 100000 by using arithmetic series.
public static int Sum(int value)
{
var n = value;
var a1 = 1;
var an = a1 + (n - 1);
var sn = n * (a1 + an) / 2;
return sn;
}
First Method
public static int GetWorkingDays(DateTime from, DateTime to)
{
var workingDay = 1 + (to - from).Days;
return Enumerable
.Range(0, workingDay)
.Select(x => from.AddDays(x))
.Count(x => x.DayOfWeek != DayOfWeek.Saturday && x.DayOfWeek != DayOfWeek.Sunday);
}
Second Method
public static double GetWorkingDays(DateTime startD, DateTime endD)
{
double workingDay = 1 + ((endD - startD).TotalDays * 5 - (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7;
if (endD.DayOfWeek == DayOfWeek.Saturday) workingDay--;
if (startD.DayOfWeek == DayOfWeek.Sunday) workingDay--;
return workingDay;
}
If your primary key is auto increment
public void InsertOrUpdate(Blog blog)
{
using (var context = new BloggingContext())
{
context.Entry(blog).State = blog.BlogId == 0 ? EntityState.Added : EntityState.Modified;
context.SaveChanges();
}
}
If your primary key is not auto increment
public void InsertOrUpdate(Blog blog)
{
using (var context = new BloggingContext())
{
var q = context.Blog.FirstOrDefault(x=>x.BlogId=blog.BlogId);
if (q != null)
context.Entry(q).State = EntityState.Detached;
context.Entry(blog).State = q == null ?EntityState.Added : EntityState.Modified;
context.SaveChanges();
}
}
If you want to convert currency string to double, there is a short way to do it.
Ex.($123.02)
var value = "($123)";
double.Parse(value, NumberStyles.Currency); //output -123.02
Asp.net Code
<asp:RadioButtonList ID="radFruits" runat="server">
<asp:ListItem Text="Mango" Value="1" />
<asp:ListItem Text="Apple" Selected="True" Value="2" />
<asp:ListItem Text="Banana" Value="3" />
<asp:ListItem Text="Guava" Value="4" />
</asp:RadioButtonList>
JQuery
var radio = $("[id*=radFruits] input:checked");
var value = radio.val();
String between two Strings
string input = "Exemple of value between two string FirstString text I want to keep SecondString end of my string";
var match = Regex.Match(input, @"FirstString (.+?) SecondString ").Groups[1].Value;
All Number between two Strings
string input = "Exemple of value between two string FirstString 1234 I want to keep SecondString end of my string";
var match = Regex.Match(input, @"FirstString (\d*) SecondString ").Groups[1].Value;