Grouping in LINQ

Posted by Mike on July 31, 2008

I’ve been using LINQ for my data access piece in .NET for the last few months and I thought I would write a quick post on how I’ve found to do grouping. I’ve seen some examples where a ‘group by new’ is done with a list several fields to group by. I’ve also seen similar examples in SQL, but this is often times not what is wanted. In SQL it generally requires a join to a second query that has the grouping field and any aggregates that are needed. In LINQ, a way that I’ve found to work for me is something like this:

var q = from invoice in context.Invoices
    where invoice.ManifestDate >= startDate && invoice.ManifestDate <= endDate
    group invoice by invoice.PoNumber into g
    select new {
        PoNumber = g.Key,
        g.First().InvoiceNumber,
        g.First().ManifestDate,
        ShippingCharge = g.Sum(p => p.ShippingCharge),
        ProductCharge = g.Sum(p => p.Quantity * p.NetPrice),
        g.First().CreateDate
    }

This allows me to group only by PoNumber in this example and calculate aggregates on ShippingCharge and ProductCharge from the group. In this example this is only going to use the first value found in the group for the value returned, which is an acceptable solution for me in this scenario.

In SQL this might look something like:

SELECT DISTINCT
    inv.PoNumber,
    inv.InvoiceNumber,
    inv.ManifestDate,
    g.ShippingCharge,
    g.ProductCharge,
    inv.CreateDate
FROM Invoice inv
JOIN (
    SELECT
        PoNumber,
        SUM(Quantity * NetPrice) as ProductCharge,
        SUM(ShippingCharge) as ShippingCharge
    FROM Invoice
    GROUP BY PoNumber) g
ON inv.PoNumber = g.PoNumber

Getting Started with IronRuby

Posted by Mike on July 27, 2008

Justin Etheredge has put together an excellent series of tutorials for setting up and getting started with IronRuby on his site.

ASP.NET MVC Videos

Posted by Mike on July 24, 2008

I’ve been checking out the MVC Storefront webcasts that Rob Conery is doing on his site. I highly recommend taking a look at them if you are interested in seeing someone build an actual application using the new MVC framework.

The webcasts that Scott Hanselman has been putting up on the asp.net/mvc site have been good too. I’m anxious to see what is next for that series.

The remote server returned an error: (550) File unavailable (e.g., file not found, no access) 4

Posted by Mike on March 03, 2008

This error came up today using FtpWebRequest in .Net. I had used the same delete method without any problems in the past, but for some reason the FTP server that I was working with was having an issue. I spent some time searching before finding one comment on an MSDN forum about KeepAlive. This turned out to be the answer. By setting FtpWebRequest.KeepAlive to false the problem was fixed and I haven’t seen it since.

private void Delete(string remoteFile) {
    string deleteRequest = ftpServer + remoteFile;
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(deleteRequest);
    request.Method = WebRequestMethods.Ftp.DeleteFile;
    request.Credentials = new NetworkCredential(ftpLoginName, ftpLoginPassword);
    request.Proxy = null;
    request.UseBinary = false;
    request.UsePassive = true;
    request.KeepAlive = false;
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader sr = new StreamReader(responseStream);
    sr.ReadToEnd();
    string StatusCode = response.StatusDescription;
    sr.Close();
    response.Close();
}

.NET 3.5 Failed to Install on Vista x64

Posted by Mike on February 10, 2008

I came across an issue while rebuilding my computer and trying to install Visual Studio 2008. I did all of the Windows updates before starting to install any software, which seems to have been the cause for this issue. When I started the Visual Studio installer, it got to where it installs the .NET 3.5 framework and eventually throws an error. After searching the Internet I found others that had the same issue. It seems to be related to a critical update for Vista, KB110806. It’s apparently a fix for .NET 2.0 that MSDN lists as being a component of .NET 3.5.

Anyway, here is what I did to get around it:

  1. Go to Windows Update
  2. Choose Change settings from the list on the left
  3. Set to "Download updates but let me choose whether to install them"
  4. Click OK
  5. Go to Control Panel
  6. Choose "Uninstall a program" under Programs
  7. Choose "View installed updates" from list on the left
  8. Find KB110806 in list, click, and choose Uninstall
  9. Reboot

The first time I tried this, I neglected steps 1 – 4 and Vista managed to reinstall the update immediately after I removed it.

At this point I downloaded and installed the .NET 3.5 framework. After that Visual Studio 2008 installed without any further issues. You may want to go back and look at steps 1 – 4 if you want to turn auto updates back on.