Showing posts with label Tweeting-How-To. Show all posts
Showing posts with label Tweeting-How-To. Show all posts

Sunday, December 23, 2012

Linq's flexibility

If you find incessant aliasing a bit too distracting...

var py =
   from p in s.Query<Product>()

   from o in p.Orders
   group o by new { p.ProductId } into grp
   select new { grp.Key.ProductId, Count = grp.Count() };


...you can do it with lambda approach:

var py =
 from p in s.Query<Product>()
  .SelectMany(_ => _.Orders)
  .GroupBy(_ => new { _.Product.ProductId })
 select new { p.Key.ProductId, Count = p.Count() };

Saturday, August 13, 2011

Make catching errors an enjoyable experience

When using Visual Studio, to put a try catch block on your code, highlight the concern code, press Ctrl+K+S, then type: try

Monday, August 1, 2011

Formatting your code on Visual Studio

On Visual Studio, if you have misaligned lines of code, to align them altogether, highlight the misaligned code code, then press Ctrl+E+F

        class MyClass
        {
    void A()
    {
            int i = 1;
        if (i == 1)
            {
                Console.WriteLine(""); }
  }
    }

Result:
    class MyClass
    {
        void A()
        {
            int i = 1;
            if (i == 1)
            {
                Console.WriteLine("");
            }
        }
    }


That functionality can also be reached under Edit > Advanced > Format Selection.

To format the whole code, press Ctrl+E+D

Monday, May 9, 2011

MySql "is distinct from"

I just saw that search phrase on my blog hit. This is MySql way:

Select * from tbl where fieldA <=> fieldB

And this is Sql Server's IS DISTINCT FROM : http://www.ienablemuch.com/2010/10/sql-server-is-distinct-from.html

Friday, April 22, 2011

My Cookbook on DTO and ViewModel

Load DTO, pass it to ViewModel(either by composition or inheritance), pass the ViewModel to View.



DTO has no behavior, it's just data; ViewModel not only has data, it also has behavior, i.e. anything seen or interacted by the user, e.g. paging, dropdown's data , and other miscellaneous information(e.g. user can see the most selling product while he/she is in data entry page, that information are passed to ViewModel) seen by users are orchestrated in ViewModel



I'm starting to believe that ASP.NET MVC should be called ViewModel-View-Controller. VMVC, any takers? View doesn't interact with pure Model anymore, View has many concerns too(e.g. paging, alert message) that if it pull all this informations by itself, the program won't look cohesive anymore.


The Controller manages this complexity by assembling all the information needed by View in one cohesive model, which is differently termed(ViewModel), so developers won't have a misconception that a View is ought to deal with several raw Model by itself.



If the ViewModel pattern is not adhered to, an unwitting developer could badly stash all Models and miscellaneous information to ViewData dictionary or ViewBag

Thursday, April 21, 2011

Visual Studio nifty shortcut

Commenting (keyboard shortcut: Ctrl+KC) is more cool when you are editing a page. When the cursor is inside of tags, it will comment the enclosing tag on both ends(i.e. beginning tag and closing tag) even you didn't highlight the beginning tag and closing tag.

<div class="editor-field">
 <table>
 <tr>                    
  <td>@Html.AjaxComboBoxFor(model => model.CategoryId,
   "/Category/Lookup",
   "/Category/Caption",
   new { style = "width: 300px " },
   new { sub_info = true } )
  </td>
  <td>@Html.ValidationMessageFor(model => model.CategoryId)</td>
 </tr>
 </table>
</div>



If you position your cursor after of <table> then you press Ctrl+KC, Visual Studio will put comment around your table tags


<div class="editor-field">
 @*<table>
 <tr>                    
  <td>@Html.AjaxComboBoxFor(model => model.CategoryId,
   "/Category/Lookup",
   "/Category/Caption",
   new { style = "width: 300px " },
   new { sub_info = true } )
  </td>
  <td>@Html.ValidationMessageFor(model => model.CategoryId)</td>
 </tr>
 </table>*@
</div>

Monday, December 20, 2010

ANY clause is superior to IN clause. At least on Postgresql :-)












Why is ANY clause superior to IN clause? Postgresql's ANY accepts array, which in turn can be manipulated further.

To illustrate the point, you cannot do this with IN expression:

select * from y 
where lower(email) IN 
    lower( ('ISteve.Jobs@Apple.com','Linus.Torvalds@Linux.com') );

Neither this:

select * from y 
where lower(email) 
    IN ( lower ('ISteve.Jobs@Apple.com','Linus.Torvalds@Linux.com') );



But with ANY clause, you can manipulate the array before passing values to it, a trick you cannot do on IN clause:

create function lower(t text[]) returns text[]
as
$$
select lower($1::text)::text[]
$$ language sql;
 
 
select * from y 
where lower(email) = 
    ANY( lower(ARRAY['ISteve.Jobs@Apple.com'
                     ,'Linus.Torvalds@Linux.com']) );

Monday, November 15, 2010

Restarting Postgresql service under OS X

sudo -u postgres /Library/PostgreSQL/9.0/bin/pg_ctl -D /Library/PostgreSQL/9.0/data restart