Friday, 31 May 2013

C# Convert List IEnumerable<T> to 2D multi-dimensional array

//extension method
public static object[,] To2DArray<T>(this IEnumerable<T> lines, params Func<T, object>[] lambdas)
{
  var array = new object[lines.Count(), lambdas.Count()];
  var lineCounter = 0;
  lines.ForEach(line =>
  {
    for (var i = 0; i < lambdas.Length; i++)
    {
      array[lineCounter, i] = lambdas[i](line);
    }
    lineCounter++;
  });
  return array;
}
 
[Test]
public void Test()
{
  var lines = new List<Line>();
  lines.Add(new Line() { Id=1, Name="One", Age=25 });
  lines.Add(new Line() { Id = 2, Name = "Two", Age = 35 });
  lines.Add(new Line() { Id = 3, Name = "Three", Age = 45 });
 
  //Convert to 2d array
  //[1,One,25]
  //[2,Two,35]
  //[3,Three,45]
  var range = lines.To2DArray(x => x.Id, x => x.Name, x=> x.Age);
 
  //test the result
  for(var i=0;i<lines.Count;i++)
  {
    for(var j=0;j<3;j++)//3 lambdas passed to function
    {
      Console.Write(range[i,j]+",");
    }
    Console.WriteLine();
  }
}
 
class Line
{
  public int Id { get; set; }
  public string Name { get; set; }
  public int Age { get; set; }
}

Thursday, 9 May 2013

MVC4 auto refresh partial view

* Create new MVC4 application and make sure you configure unobtrusive-ajax as described in How to use MVC3 with AJAX

* Create Controller /Controllers/HomeController.cs

public class HomeController : Controller
{
  public ActionResult Index()
  {
    var model = new ViewModel();
    model.Now = DateTime.Now.ToString();
    return View(model);
  }
 
  public ActionResult Refresh()
  {
    var model = new ViewModel();
    model.Now = DateTime.Now.ToString();
    return PartialView("IndexPartial", model);
  }
}
 
public class ViewModel
{
  public string Now { get; set; }
}

* Create view /Views/Home/Index.cshtml

@using MVCTest.Controllers
@{
  ViewBag.Title = "Index";
}
<h2>Index</h2>
<a id="button" title="Refresh now">Refresh Now</a>
<a id="toggleButton" title="Auto refresh every 5 seconds">Auto Refresh</a>
 
<div id="PartialDiv">
  @Html.Partial("IndexPartial", (ViewModel)Model)  
</div>
 
<script type="text/javascript">
 
  var autoRefresh = false;
  var autoRefreshInterval = 1000 * 5;   //mili seconds (set to 5 seconds)
 
  $(document).ready(function () {
    $('a#button').click(function () {
      $('#PartialDiv').load('@Url.Action("Refresh", "Home")');
    });
 
    $('a#toggleButton').click(function () {
      $('#PartialDiv').load('@Url.Action("Refresh", "Home")');
      $(this).toggleClass("down");
      if ($(this).is('.down')) {
        autoRefresh = true;
      } else {
        autoRefresh = false;
      }
    });
 
    var refresh = function () {
      if (autoRefresh) {
        $('#PartialDiv').load('@Url.Action("Refresh", "Home")');
      }
      setTimeout(refresh, autoRefreshInterval);
    };
 
    refresh();
  });
</script>

* Create partial view /Views/Home/IndexPartial.cshtml

<h2>@Model.Now</h2>

* Add these css classes to your Site.css file

#button {
  cursor: pointer;
  font-weight: bold;
}
 
#toggleButton {
  color: darkred;
  font-weight: bold;
  cursor: pointer;
}
 
#toggleButton.down {
  color: green;
  font-weight: bold;
  cursor: pointer;
}

* The webpage should look like this with manual Refresh Now button and Auto Refresh toggle button

mvc4-autorefresh

Wednesday, 8 May 2013

Remote debugging from Visual Studio

Copy RemoteDebugger to remote machine from your local Visual Studio directory

(This is path for VS2010)

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Remote Debugger

Start remote debugging monitor on the server ‘msvsmon.exe’

Go to Tools/Options and select ‘No Authentication (native only)’ and ‘Allow any user to debug’

remote-debugging

In your Visual Studio select ‘Debug/Attach to Process’, specify Transport as ‘Remote (Native only with no authentication)’ and put your server name as Qualifier, click Refresh and you should see the list of processes on the remote machine.

Select the process you want to debug and click ‘Attach’

Using NuGet without committing packages to source control

Using NuGet without committing packages to source control