Posts

Showing posts from November, 2014

LINQ - Left join with where conditions

Left joins don't come natural to LINQ but the solution is not that complicated. Below is my example I needed to pass in 3 fields for my where clause and I was getting back the LoginName field from the USERS table - unfortunately sometimes this field was empty and my join would not work - in the SQL we know and love this is easily accomplished with a left join (take all the fields from the first table or the table on the left and any that match in the second table) Below you can see how this works. The 'subgroup' referenced below is just an alias, there is no table named subroup - almost acting like a temptable.  var mygroupitems = (from e in MyEntities.Terr_History                             join p in MyEntities.Users on e.Approver_User_Id  equals p.Users_Id into XX                             from subgroup in XX.DefaultIfEmpty()                             where e.tdg_PID == pid  && e.Account_Code==accountcode                             && e.

How to get Commas in your razor Html.TextBoxFor

There are so many ways to do this ... but depending on circumstances they don't always work.  The simplest way is to apply the format directly on the Textbox like this:   @Html.TextBoxFor(model => item.Population, "{0:N0}", new {style = "width: 100px;"})

The Simple Answer to the WebGrid sorting issue - LINQ to Entities only supports casting EDM primitive or enumeration types

Spent so much time on this error, examining my models, data structure etc. What it boiled down to was the sortability of the IEnumerable object on the page vs a sortable list. All you need to do most of the time is to recast it in your controller like this:  var retdata = bl.GetPidHistory(pid);  var itemstoreturn = retdata.ToList();  return View(itemstoreturn);  This is applicable when your view has a model like this: @model IEnumerable