In
Syntax:
public static bool In(this T e, params T[] list)
{
return list.Any(l => e.Equals(l));
}
Example:
var array = new [] {1,2,3,4};
var index = 1;
if (index.In(array))
// do some action
public static T ToEnum(this string value)
{
return (T)Enum.Parse(typeof(T), value);
}
public enum SessionKey
{
IsLoggedIn, ReportId
}
//---------------
var myString = "ReportId";
switch (myString.ToEnum())
{
case SessionKey.ReportId:
// do some action
case SessionKey.IsloggedIn:
// do some action
}
More to learn:
BEGIN T1
INSERT INTO [ReportSystem] ([ReportSystemId], [ReportSystemName], [BaseReportUrl]) VALUES(2, 'QlikView', NULL);
IF (@@ERROR <> 0) GOTO DoRollBack
DELETE FROM ReportSystem
IF (@@ERROR <> 0) GOTO DoRollBack
DELETE FROM FilterParameterType
IF (@@ERROR <> 0) GOTO DoRollBack
COMMIT T1
RETURN
DoRollBack:
ROLLBACK TRAN T1
PRINT 'Rolled back'
BEGIN T1
declare @rc int;
declare @sql nvarchar(500);
set @sql = N'DELETE FROM FilterParameterType; DELETE FROM ReportSystem;';
exec @rc = sp_executesql @sql;
if @rc <> 0 ROLLBACK T1
COMMIT T1
Approach1: Using INSERT VALUES
INSERT INTO Table1 (FirstCol, SecondCol)
VALUES ('First',1);
INSERT INTO Table1 (FirstCol, SecondCol)
VALUES ('Second',2);
INSERT INTO Table1 (FirstCol, SecondCol)
SELECT 'First',1;
INSERT INTO Table1 (FirstCol, SecondCol)
SELECT 'Second',2;
INSERT INTO Table1 (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
INSERT INTO Table1 (FirstCol, SecondCol)
SELECT FirstName, number FROM Table2
SELECT FirstName, LastName
INTO TempTable
FROM Person.Contact
Approach 6: Using SELECT INTO – Permanent table
The following statement inserts the data from an existing table named “Table2” into another existing table named “Table1”:
SELECT FirstName, LastName
INTO db1Name.dbo.Table1
FROM dbo2Name.dbo.Table2
Microsoft Expression Studio provides a set of professional design tools for .NET web, desktop, WPF or Silverlight applications.
This package contains Expression Web, Expression Blend, Expression Design and Expression Encoder and SketchFlow tools.
For instance, your professional designer can design a rich UI using Expression tools and produce a XAML result. Then your professional WPF developer gets the XAML and develops code and logic for it.
To build websites; similar to DreamWeaver and FrontPage but with more features and supports AJAX, C#, etc.
Using which you can create rich UI (e.g. 3D) for .NET, WPF and Silverlight.
Using which you can build designs and vector assets (similiar to Photoshop but with different features)
For instance you can publish videos from your webcam to Silverlight. You can also customize your videos; adding titles, etc.
It is a great tool for prototyping.
For a full list of features, see this.
public static string GetFromWebConfig(WebConfigKey key)Note: WebConfigKey is an "enum"
{
var str = ConfigurationManager.AppSettings[key.ToString()];
if (str == null)
{
throw new ApplicationException("Key: " + key.ToString() + " does not exist in the web.config");
}
return str;
}
public interface IConfigurationManager
{
NameValueCollection AppSettings {get;}
}
public class ConfigurationManagerWrapper : IConfigurationManagerNote: the above class uses a thread-safe singleton pattern
{
#region Fields and Properties
static ConfigurationManagerWrapper instanceHolder = new ConfigurationManagerWrapper();
public static ConfigurationManagerWrapper Instance
{
get { return instanceHolder; }
}
public NameValueCollection AppSettings
{
get
{
return ConfigurationManager.AppSettings;
}
}
#endregion
#region Constructors
private ConfigurationManagerWrapper()
{
}
#endregion
}
public static string GetFromWebConfig(WebConfigKey key)Note: the first method is to be used directly by the production code and passes a singleton instance of ConfigurationManagerWrapper. The second code will be used directly by the testing framework to benefit from dependency injection.
{
return GetFromWebConfig(key, ConfigurationManagerWrapper.Instance);
}
public static string GetFromWebConfig(WebConfigKey key, IConfigurationManager configurationManager)
{
var str = configurationManager.AppSettings[key.ToString()];
if (str == null)
{
throw new ApplicationException("Key: " + key.ToString() + " does not exist in the web.config");
}
return str;
}
[TestMethod()]Note: NMock has been used.
public void GetFromWebConfigTest()
{
// setup
var key = WebConfigKey.SecondsToCache;
var expected = "60";
mocks = new Mockery();
var configurationManager = mocks.NewMock();
Expect.Once.On(configurationManager).GetProperty("AppSettings")
.Will(Return.Value(new NameValueCollection {{key.ToString(), expected}}));
// exercise
var actual = Utils.GetFromWebConfig(key, configurationManager);
// verify
Assert.AreEqual(expected, actual);
mocks.VerifyAllExpectationsHaveBeenMet();
}
using System;
using System.Linq;
using System.Reflection;
using System.Text;
/// <summary>
/// Summary description for EFMetadataGenerator
/// </summary>
public class EFMetadataGenerator
{
/// <summary>
/// </summary>
/// <param name="fromNamespace">This is the namespace of your EntityFramework</param>
/// <param nam/// </summary>e="toFile">This is the path of the file on which you want the results to be saved</param>
public static void Generate(string fromNamespace, string toFile)
{
var result = new StringBuilder();
result.Append(AddHeader(fromNamespace));
var modules = Assembly.GetExecutingAssembly().GetModules();
foreach (var module in modules)
{
var types = module.GetTypes().Where(t => t.Namespace == fromNamespace);
foreach (var type in types) {
result.Append(GenerateMetadataClasses(type));
}
}
result.Append(AddFooter());
System.IO.File.WriteAllText(toFile, result.ToString());
}
private static string AddHeader(string namespaceName)
{
var sb = new StringBuilder();
sb.Append("using System;" + GetNewLine(1));
sb.Append("using System.ComponentModel;" + GetNewLine(1));
sb.Append("using System.Data;" + GetNewLine(1));
sb.Append("using System.Data.Objects;" + GetNewLine(1));
sb.Append("using System.ComponentModel.DataAnnotations;" + GetNewLine(1));
sb.Append(GetNewLine(1) + "namespace " + namespaceName + GetNewLine(1)); sb.Append("{" + GetNewLine(1));
return sb.ToString();
}
private static string AddFooter() { return "}"; }
private static string GenerateMetadataClasses(Type type)
{
var className = type.Name;
var metadataClassName = type.Name + "_MD";
var sb = new StringBuilder();
sb.Append(GetTab(1) + "#region " + className + GetNewLine(2));
// generate the partial class
sb.Append(GetTab(1) + "[MetadataType(typeof(" + metadataClassName + "))]" + GetNewLine(1));
sb.Append(GetTab(1) + "public partial class " + className + GetNewLine(1));
sb.Append(GetTab(1) + "{" + GetNewLine(1) + GetTab(1) + "}" + GetNewLine(2));
// generate the metadata class
sb.Append(GetTab(1) + "public class " + metadataClassName + GetNewLine(1));
sb.Append(GetTab(1) + "{" + GetNewLine(1));
var properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
sb.Append(GetTab(2) + "public object " + propertyInfo.Name + " { get; set; }" + GetNewLine(1));
}
sb.Append(GetTab(1) + "}" + GetNewLine(2));
sb.Append(GetTab(1) + "#endregion " + className + GetNewLine(2));
return sb.ToString();
}
private static string GetNewLine(int count)
{
var sb = new StringBuilder();
for (var i = 0; i < count; i++) sb.Append("\r\n");
return sb.ToString();
}
private static string GetTab(int count)
{
var sb = new StringBuilder();
for(var i = 0; i< count; i ++) sb.Append("\t");
return sb.ToString();
}
}
Problem:
I have the following property on my entity framework designer that I want to make it ReadOnly:
[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]Solution:
[global::System.Runtime.Serialization.DataMemberAttribute()]
public int FilterParameterId
{
get
{
return this._FilterParameterId;
}
set
{
// etc
}
}
[MetadataType(typeof(FilterParameter_MD))]
public partial class FilterParameter
{
}
public class FilterParameter_MD
{
[UIHint("ReadOnlyText")]
public object FilterParameterId { get; set; }
}
Have you ever wondered how to simulate HttpContext so that you can write your unit tests? TypeMock can do that but it's not a free tool.
HttpSimulator is a component composed of 2 classes; I am not sure whether Phil Haak has written it himself but it solves that problem nicely. You can download it from the bottom of this post.
Code sample:public static HttpSimulator CreateHttpSimulator()
{
return new HttpSimulator("/", @"c:\inetpub\").SimulateRequest();
}
[TestMethod()]
public void SetSessionTest()
{
using (Helper.CreateHttpSimulator())
{
// setup
var expectedValue = 2;
// exercise
Utils.SetSession(SessionKey.Basket, expectedValue);
// verify
var actualValue = Utils.GetFromSession(SessionKey.Basket);
Assert.AreEqual(expectedValue, actualValue);
}
}
public static void SetSession(SessionKey key, object value)
{
HttpContext.Current.Session[key.ToString()] = value;
}
public static object GetFromSession(SessionKey key)
{
if (HttpContext.Current == null HttpContext.Current.Session == null)
throw new ApplicationException("HttpContext.Current.Session is null");
if (HttpContext.Current.Session[key.ToString()] == null)
return null;
return HttpContext.Current.Session[key.ToString()];
}
Collaborative Application Markup Language, or CAML for short, is an XML-based language that SharePoint uses for queries, configuration, and rendering of data in addition to other uses. CAML has some similiarities to SQL in function.
CAML has several subsets, called schemas, that are used for different tasks. For example the View schema is used for defining visual rendering of data while the Query schema is used for extracting data.
http://msdn.microsoft.com/en-us/library/ms467521.aspx
http://msdn.microsoft.com/en-us/library/ms426449.aspx