Archives

Archives / 2009 / September
  • LINQ Having Clause and Group By with condition

    Hi,

    While querying with LINQ, some times we will have to use the group By clause. But many a times we also want to use the having clause of SQL with the group by clause in the LINQ. There is no direct having keyword in LINQ to do this we need to use the where clause itself.

    You can query the LINQ to SQL to have both Group By and Having like this.

    var categories =   from p in db.Products
                   group p by p.CategoryID into g
                   where g.Count() >= 10
                   select new {
                          g.Key,
                          ProductCount = g.Count()
                         };

    But there are occasion when you might not to group with an existing column but with a column which calculated at the run time itself. For those conditions you can write the query like this.

    var categories =   from p in db.Products
     
                              group p by new { Criterion = p.UnitPrice > 10 } into g
                               where g.Count() >= 10
                               select new {
                                        g.Key,
                                        ProductCount = g.Count()
                                       };

    Vikram

  • How to simulate IN Clause in LINQ queries

    Hi,

    When working with the LINQ queries for SQL one of the common queries that we need to run is the select query with IN clause. In SQL IN clause is used to provide more than one value to match in the where clause.

    Something like the query below

    Select * from Table
    where column1 in (‘Value1’, ‘Value2’, ‘Value3’)

    To do a similar query in LINQ we can use

    var list = from t in table
                    where t.column1 = Value1’
                    And t.column1 = Value2’
                    t.column1 = Value3’
                    Select t

     But what if the number of values are not known is design time, coming from another list etc?

     In that case you need to write the LINQ query like this.

    List<string> names = new List<string>();
       names.Add("Value1");
       names.Add("Value2");
       names.Add("Value3");
       names.Add("Value4");

     var list = from t in table
                   where names.contains(t.column1)
                   select t

    You can pass the value of a full table also by querying the table first and then fetching all the values of the table in the list.

    Vikram

  • A workshop for IT Professionals on 15th September on Implementing SECURITY INSIDE-OUT with MICROSOFT Technologies

     Hi,

    There an interactive live session on September 15, 2009 on how the New Efficiency Products will help you manage security in a simpler way. This session will occur across 16 cities at NIIT centers as a live session with benefit of face-to-face teaching, each participant having their own computers to work on, and having the ability to ask questions any time. The following event is available for the following city.

  • Application running in localhost and debugging mode

    Hi,

    Most of the time (If not all) when working on the project we view the website with the help of localhost in debugging mode. Many a times when working on some modules, for solving small problem, we want to write some code that should execute only when running in the localhost or debugging environment.

    Normally developers would write the code and then take it as their responsibility to remove the code before the code goes to the next environment. But some time we might make a mistake of not removing the code and letting it go to the next environment, which can cause a lots of trouble.

    In these kinds of circumstances and Asp.Net developer can take help of the IsLocal method of the Request class. The method will return true in case the application is running from localhost.

    If (Request.IsLocal)
    {
    // Do task that should execute only when running in localhost.
    }

    To know if the application is running in the debugging mode, we can use the

    If (HttpContext.Current.IsDebuggingEnabled)
    {
    // Do task that should execute only when Application is running in debugging mode.
    }

    Vikram

  • Visual Studio Changing default profile setting

    Hi,

    While working with Visual Studio, was that normally we have to select the profile setting (Visula Basic profile, C# profile, web development profile etc...) when we start the Visual Studio for the first Times. But Many a time after working on a type of project for some time you start working on other types of project.

    For Example, Currently I am working on a Windows Applictaion (With Language VB.Net) But before this project I was Working on Web Development in C#, and before that Windows Development in C#.

    In Visual studio you can easily change the profile of the IDE by going to Tools -> Import Export Settings. This brings up Import Export Windows Wizard. Follow the simple Wizard (which also allows to save you the current settings  for future use) and seletc the new profile setting (based on the kind of project you are working on) and you are ready to work with new profile.

    Vikram