Posts

SubSelects in LINQ

What a challenge I was facing trying to figure out how to solve this problem in LINQ.. but i did it .. and am sharing. SQL query: Select Distinct  o.ConceptCode, u.firstname, u. lastname , (select count(*)  from callLead  where calldate ='2014-09-02'  and conceptcode=O.conceptcode and userid=O.userid) 'DailyTotal' ,(select count(*) from callLead where calldate between '2014-09-01'  and '2014-09-05' and conceptcode=O.conceptcode and userid=O.userid) 'WTD' ,(select count(*) from callLead where datepart(m,calldate)=9 and conceptcode=O.conceptcode and userid=O.userid) 'MTD' ,(select count(*) from callLead where datepart(yyyy,calldate)=2014 and conceptcode=O.conceptcode and userid=O.userid) 'YTD' from callLead O join users u on o.userid=u.userid The LINQ Conversion: The LINQ version is much prettier actually  var rptSummary = (from e in SalesEntities.CallLeads                         ...

Cannot Convert Int to String - MVC Dropdownlist Nightmare

So I struggled with this simple code for binding a dropdownlist IEnumerable items = db.SiteMenus               .Select(c => new SelectListItem  {  Value = c.MenuID,  Text = c.MenuName               });                  ViewBag.SiteMenuItems = items;               return View();                .Select(c => new SelectListItem   {              Value = c.MenuID,                                Text = c.MenuName               });              ViewBag.SiteMenuItems = items; return View();             list.Add(new SelectListItem { Text = "-Select Catego...

Microsoft Deserves Some Credit

I am the first to point out system deficiencies and am happy to let you know that this morning I am ragging on no one. I have a new Surface Pro 3 and am loving it as McDonalds might say, this morning I am very happy with Microsoft for a different reason. At work we are using Team Visual Studio - phenomenal product, especially for remote workers.  My work computer, which is a mean laptop with 16 gigs of ram and a kick butt processor has Visual Studio 2012.  This morning I wanted to revisit a project I was working on so decided to try and connect my surface with VS 2013 to the project.  It was so absolutely seamless that it put a happy spin on the day ahead! Congrats to the Visual Studio Online team. So after investigating it looks like with VS 2012 Microsoft did something brilliant - they made the Visual Studio interface backward compatible thus not forcing an office to upgrade all developer software at once but more importantly allowing me to work at 5 in the mornin...

Membership and Looping through the Site Users in MVC with Razor

There are so few examples of this out there - it took me forever to find it but it was such a simple, elegant solution that I thought I would share. So in your Action you don't want to use the MembershipCollection directly as it will return a string that will not be IENumerable - instead you will want to use your UsersContext This will go in your Controller     [Authorize(Roles = "Admin")]         public ActionResult MembersIndex()         {             using (var ctx = new UsersContext())             {                 return View(ctx.UserProfiles.OrderBy(x => x.UserId).ToList());             }         } Then in your Razor MVC Page you will do something like this:       @foreach ( var user in Model){         @user.UserName  - @...

An Open Letter to Microsoft

Dearest Microsoft, We have been the best of friends for many years.  I have built my career around your technologies, been an advocate for your software, shared information on your hardware and participated in your MVP program (twice!).  It is out of this deep appreciation for you that I write to you today. I just purchased the Surface Pro 3 - and I love it - but that is not what this letter is about. Your store (microsoftstore.com) could use a revamp.. The deficiencies as I see them are primarily with the customer specific pages, once the customer is signed in. The view on the primary page and the order page is not in a grid as one might expect when supplying this type of information nor is it laid out in a manner easily read, in fact the page is so poorly designed that if you have three orders with 3 items each you have to actually go to the next page to see the third order,  I can't imagine the difficulty if someone had 10 or more orders.  Not only is th...

Data Entity Random Database Record

You've seen those random quotes on pages, probably most of them are done with Javascript.  This one is done using a database table (three fields, id, linenumber,linetext) and a data entity call and features the SKIP method. You have to have an orderby in your query to be able to use SKIP So put a label on the page or webcontrol and format it as you wish From the page load call the function to get the data lblRandom.InnerText = getrandomvalue();  public string getrandomvalue()         {             using (my_maintenanceEntities context = new my_maintenanceEntities())             {                 var query = (from c in context.tbl_texttable                              orderby c.LineNumber                       ...

Another Conditional Linq for int values

I have a dropdown list where the first selection is to return ALL Records and the value is 0 otherwise they make a selection and it returns the equivalent integer. Here is how I worked it out in my Linq query The dropdown is passing into a variable called ClassCodeID  && (ClassCodeID.Equals(0) || s.ClassCodeID >= ClassCodeID || s.ClassCodeID == ClassCodeID) And that seemed to work... however when returning values it did not roll up the values as I expected since classcodeID was not in the groupby.. it only returned a record for a specific code but the values from all codes were rolled into the returned data.  I added an additional where statment and this seemed to have fixed the issue . where (ClassCodeID.Equals(0) || s.ClassCodeID == ClassCodeID