Wednesday, September 17, 2014

Linq in JavaScript?

Do you miss Linq in JavaScript? qArr.js offer some basic Linq query features when working with arrays in JavaScript.

Library: https://github.com/waltersoto/qArr.js 

To illustrate how qArr.js works we'll do some examples:


First, we need a dataset to query against; so, let's create an array with information about the top 10 box office movies of all time.

var allTimeBoxOffice = [
        { rank: 1, title: 'Avatar', studio: 'Fox', grossInB: 2788, year: 2009 },
        { rank: 2, title: 'Titanic', studio: 'Paramount', grossInB: 2186.8, year: 1997 },
        { rank: 3, title: 'Marvel\'s The Avengers', studio: 'Buena Vista', grossInB: 1518.6,           year: 2012 },
        { rank: 4, title: 'Harry Potter and the Deathly Hallows Part 2'
          studio: 'Warner Bros.', grossInB: 1341.5, year: 2011 },
        { rank: 5, title: 'Frozen', studio: 'Buena Vista', grossInB: 1274.2, year: 2013 },
        { rank: 6, title: 'Iron Man 3', studio: 'Buena Vista', grossInB: 1215.4, 
          year: 2011 },
        { rank: 7, title: 'Transformers: Dark of the Moon', studio: 'Paramount',
          grossInB: 1123.8, year: 2011 },
        { rank: 8, title: 'The Lord of the Rings: The Return of the King'
          studio: 'New Line', grossInB: 1119.9, year: 2003 },
        { rank: 9, title: 'Skyfall', studio: 'Sony', grossInB: 1108.6, year: 2012 },
        { rank: 10, title: 'The Dark Knight Rises', studio: 'Warner Bros.'
          grossInB: 1084.4, year: 2012 }


Querying movies from 2011 using .where().

var from2011 = qA(allTimeBoxOffice).where(function (x) { return x.year === 2011; }).toArray();


The example above will generate the following dataset:

[
 {"rank":4,"title":"Harry Potter and the Deathly Hallows Part 2","studio":"Warner Bros.","grossInB":1341.5,"year":2011},
 {"rank":6,"title":"Iron Man 3","studio":"Buena Vista","grossInB":1215.4,"year":2011},
 {"rank":7,"title":"Transformers: Dark of the Moon","studio":"Paramount","grossInB":1123.8,"year":2011}
]

Now, lets select a list of movie titles sorted by year in descending order by using .select() and .orderByDescending():

var titles = qA(allTimeBoxOffice).select(function (x) { return x.title; })

                     .orderByDescending(function (x) { return x.year; }).toArray();

The example above will generate the following dataset:

["Transformers: Dark of the Moon","Titanic","The Lord of the Rings: The Return of the King","The Dark Knight Rises","Skyfall","Marvel's The Avengers","Iron Man 3","Harry Potter and the Deathly Hallows Part 2","Frozen","Avatar"]

As a last example, lets group and sort the dataset using year as a category with .groupBy() and .orderBy().

   var groupedByYear = qA(allTimeBoxOffice).groupBy(function (x) { return x.year; })
                                                .orderBy(function (x) { return x.key; })
                                                .toArray();

The example above will generate the following dataset:
[
  {"key":1997,"item":[{"rank":2,"title":"Titanic","studio":"Paramount","grossInB":2186.8,"year":1997}]},

  {"key":2003,"item":[{"rank":8,"title":"The Lord of the Rings: The Return of the King","studio":"New Line","grossInB":1119.9,"year":2003}]},
  
  {"key":2009,"item":[{"rank":1,"title":"Avatar","studio":"Fox","grossInB":2788,"year":2009}]},
  
  {"key":2011,"item":[{"rank":4,"title":"Harry Potter and the Deathly Hallows Part 2","studio":"Warner Bros.","grossInB":1341.5,"year":2011},{"rank":6,"title":"Iron Man 3","studio":"Buena Vista","grossInB":1215.4,"year":2011},{"rank":7,"title":"Transformers: Dark of the Moon","studio":"Paramount","grossInB":1123.8,"year":2011}]},

  {"key":2012,"item":[{"rank":3,"title":"Marvel's The Avengers","studio":"Buena Vista","grossInB":1518.6,"year":2012},{"rank":9,"title":"Skyfall","studio":"Sony","grossInB":1108.6,"year":2012},{"rank":10,"title":"The Dark Knight Rises","studio":"Warner Bros.","grossInB":1084.4,"year":2012}]},

  {"key":2013,"item":[{"rank":5,"title":"Frozen","studio":"Buena Vista","grossInB":1274.2,"year":2013}]}
]



Thursday, February 23, 2012

Free Intro To JavaScript Book Online

If you need a easy to follow JavaScript into book and don't mind having to read it off your monitor screen you can check the free online version of Sams Teach Yourself JavaScript in 24 Hours.


Wednesday, September 14, 2011

Microsoft BUILD 2011 Keynote #1

Microsoft keynote featuring Steven Sinofsky, Mike Angiulo and Julie Larson-Green talking about Windows 8.

Wednesday, August 10, 2011

Android Tip: Enable a progress bar for a WebView

This is how you enable a progress bar when loading web pages using a WebView in Android:


final Activity activity = this;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
     
       
        String url = "http://m.bing.com";

        WebView web = (WebView) findViewById(R.id.mainView);
             web.getSettings().setJavaScriptEnabled(true); 
             web.getSettings().setBuiltInZoomControls(true);
             web.getSettings().setUseWideViewPort(true);
              web.setWebChromeClient(new WebChromeClient() {
                  public void onProgressChanged(WebView view, int progress) {
                        activity.setProgress(progress * 100);
                  }
              });

             web.loadUrl(url);
       
    }

Note: requestFeature() must be call before any other content is added (call it right after super.onCreate).


Thursday, June 30, 2011

Mango Beta 2 for WP7 available for developers

I received an invitation to participate in the WP7's Mago 'Beta 2' program; unfortunately, I don't have a WP7 device (I use the emulator to develop). So, at least it seems that MS may be ready to roll out the first WP7.5 aka Mago phones by the end of the year.

Wednesday, June 1, 2011

How to connect to a SQL server database in c#

The following code snippet will show you how to query an SQL Server database in C# using ADO.NET.
Namespaces you will need to include:
using System.Data;
using System.Data.SqlClient;

Source Code:
SqlConnection conn = new SqlConnection("Data Source=Server Name;UID=User Name;PWD=Password;Initial Catalog=Database Name");
           SqlCommand command = new SqlCommand("SELECT TOP 100 * FROM  tbl_Users",conn);
           DataTable dt = new DataTable();

            command.Connection.Open();
           SqlDataAdapter adapter = new SqlDataAdapter(command);
           adapter.Fill(dt);

           adapter.Dispose();
           command.Connection.Close();
           command.Dispose();

           foreach (DataRow dr in dt.Rows)
           {
               Console.WriteLine(dr["FirstName"].ToString()+" "+dr["LastName"].ToString());
           }

Breakdown
Initialize a SqlConnection object by passing you server connection string as a parameter. Then, initialize a SqlCommand object which you can use it to execute a SQL query or a stored procedure (We passed the SqlConnection object as an additional parameter).  I will be retrieving a set of data from the sample query; so, I need either a DataSet or DataTable object to store the result. Next, we initialize a SqlDataAdapter object that will serve us as a bridge between the SqlCommand and the DataTable (we passed the SqlCommand object as a parameter). Finally, we simply open a connection and call the Fill() method to retrieve our result. 

Thursday, May 19, 2011

.NET Rocks Podcast

Software development podcasts are a great way to keep up with the always changing tech industry.

.NET Rocks Podcast is my favorite podcast about software development and not because I work with .NET but overall quality of the show is presented. The show is hosted by Carl Franklin and Richard Campbell; where they conducts a weekly interview to  industry experts such as Scott Guthrie and Billy Hollis.

To listen:

http://www.netcastia.com/dotNETRocks

or

http://www.dotnetrocks.com/