Tuesday, September 16, 2014

Console.WriteLine equivalent in ASP.Net

System.Diagnostics.Debug.WriteLine("Message");

Messages are displayed in output window

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

try
{
    //...........
    dc.SaveChanges();
}
catch (DbEntityValidationException ex)
{
    foreach (var eve in ex.EntityValidationErrors)
    {
        System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }
    }
}
Look for errors in the output window during debugging

LINQ Lamda Syntax - Selecting multiple columns

use the new keyword
var parent = dc.Forms.Select(f => new { f.FormID, f.ModuleId })

How to check if string contains a string from a string array

Using LINQ Any
string[] rpts = { "rpt""rtp""Rpt""Report" };
using if and any to check whether string contains any item from array
if (rpts.Any(url.Contains))
{
    ........
}