- Posted by Ian Suttle on December 22, 2007
- Filed under .Net Framework | C# 3.0
Tonight I'm playing around with LINQ to SQL using the AdventureWorks database. I'm going to load a product from the Product table given the ProductID, therefore loading a single product. Simple right? There are a couple of was to construct a LINQ statement to retrieve a record:
LINQ Query
public Product GetProduct(int productID)
{
AdventureWorksDBDataContext db = new AdventureWorksDBDataContext();
Product product = (from p in db.Products
where p.ProductID == productID
select p).Single();
return product;
}
Using a Lambda expression
public Product GetProduct(int productID)
{
AdventureWorksDBDataContext db = new AdventureWorksDBDataContext();
Product product = db.Products.Single(p => p.ProductID == productID);
return product;
}
If you notice I'm using the Single() extension method, which will absolutely do the job... when the record exists. The gotcha is if the record doesn't exist an exception is thrown. Instead of using the Single() method, use SingleOrDefault(). This is one you'll learn the hard way... I did. Here's a sample using the Lambda expression without worrying about an exception when the record is missing.
Using a Lambda expression with SingleOrDefault
public Product GetProduct(int productID)
{
AdventureWorksDBDataContext db = new AdventureWorksDBDataContext();
Product product = db.Products.SingleOrDefault(p => p.ProductID == productID);
return product;
}
- Posted by Ian Suttle on December 10, 2007
- Filed under .Net Framework
I had an instance where I was using a custom class as a return value type in a web service. This is a pretty typical scenario. When a web service is consumed, you, as the author, may want to alter the names of the properties being returned and would do so as such:
[XmlElement("NewName")]
public string InternalName
{
get { return "something here..."; }
}
When serialized you'd have an element of <NewName /> somewhere in there.
...or if you have a class being return
[XmlRoot("NewClassName")]
public class InternalClass
{
...
When serialized you'd have a root node of <NewClassName><OtherNode...
You get the picture, right? So far so good. Now let's complicate matters a bit (as complicated as it gets at least). Say you want to return a generic list of InternalClass defined something like this:
public List<InternalClass> InternalClassList
{
get {...
Using the XmlRoot attribute on the class no longer works. You would see <InternalClass> instead of <NewClassName> So how do you modify the name of the generic parent and it's children? Treat it as an array!
[XmlArray("NewClassNames")]
[XmlArrayItem("NewClassName")]
public List<InternalClass> InternalClassList
{
get {...
Once serialized you would have a favorable result such as:
<NewClassNames>
<NewClassName />
<NewClassName />
</NewClassNames>
- Posted by Ian Suttle on December 10, 2007
- Filed under .NET 3.5 | ASP.Net | .Net Framework
It's late, I'm finally catching Cinderella Man, and I'm heading for vacation this week... of course none of that is interesting to you, but the
CTP release of the ASP.NET 3.5 Extensions CTP Preview probably is. The MVC framework seems to have garnered the most blog entries as of late. I'll be sure to check it out when I return, unless the wife falls asleep early in the hotel and I can sneak the laptop out that is!