Posts

Showing posts from September, 2015

Cast Nullable Ints inside your Select in Linq

Needed to return a single value and cast it as an int instead of an int?     public int getFranchiseePOS(string AccountCode)         {             var qry = (from s in MyEntities.Franchisees                        join p in MyEntities.POS on s.POSID equals p.POSID into XX                        from subgroup in XX.DefaultIfEmpty()                        where s.AccountCode == AccountCode                        select s.POSID==null ? 0 :(int) s.POSID                       ).FirstOrDefault();             return qry;         }

Best Practices

This article is a bit dated but I believe it is still applicable: Best Practices from MSDN

DataEntities & Stored Procedures

Image
There are lots of ways to connect to your data. I really like working with DataEntities but I still appreciate the efficiency of stored procedures, so don't forget that they can work peacefully together. For this example I have selected a stored procedure that does some behind the scenes manipulation. It compares a table that I have just added records to from an Excel upload and it inserts those records into another table if they don't exist already, then it deletes the initial group of records. It also returns the number of records that it moved from one table to the other. Create PROCEDURE [dbo].[UpdateExtensions] --This is the output parameter that we will pass from the method which will return the number of changed records @row_count int OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. INSERT Account_StoreExtensions (AccountCode, ExtensionURL, Store_Extension) Select AccountCode, ExtensionURL

Best way to Read Excel Files with MVC

There of course are multiple ways to upload and read an Excel file.  After creating a version the long way I ran into file storage, permission issues etc so started looking for a better way to do this. I found a solution that does not actually upload the excel file, so no storage issues, no permission issues ... and a lot less code.  I am using a package available from Nuget called EPPlus.  So first thing you will want to do is grab this package - here is the nuget command line: install-package EPPlus Be sure and add this to your Controller: using OfficeOpenXml; public ActionResult UploadExtensions(HttpPostedFileBase file) { if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName)) { string fileName = file.FileName; string fileContentType = file.ContentType; byte[] fileBytes = new byte[file.ContentLength]; var data = file.InputStream.Read(file

A real world query with where's and ifs and nulls and multiple joins oh my!

I was looking for a good example of a Join but the examples I found were simple and did not include the other elements such as the where statement and multiple tables. So here is an example that works that includes those elements. I have eliminated some of the fields if you are wondering why I have so many joins that I didn't appear to use. The following query has multiple joins, one of the joins is joining on two columns with a defaultifempty applied - essentially making it a left join. One of the fields is an integer but I wanted to switch out a string instead - hopefully this will help someone who is looking for the answer for one of these linq constructors. MyEntities tfs = new MyEntities(); var rptCon = (from e in tfs.Company_Zips join b in tfs.Franchisees on e.AccountCode equals b.AccountCode join c in tfs.Account_StoreExtensions on new{ e.Zip_StoreExtensionID,e.Accountcode} equals new{ c.StoreExtensionID,c.AccountCode} into ex join p in tfs.Postal_Code_Territor