| Very Quick Guide To DLinq: Part 3 (LIKE, IN, DATEDIFF) | Posted on Wednesday, April 30, 2008 at 6:50 AM | Much of the time, it's fairly straightforward to express an SQL query in Linq instead. However, there are some things that aren't quite so clear how to do. In this article, I'll take a look at some of those.
LIKE
There are a couple of ways to replace LIKE. If you just want to directly use LIKE, then you can do so using the SqlMethods class. To use this, you must add the following using declaration at the top of the file:
using System.Data.Linq.SqlClient;
Then we can use it like this:
var Result = from Book in DB.BookResources
where SqlMethods.Like(Book.Title, "%java%")
select Book;
This will create an SQL query that locates all books containing "java" (case-insensitively). Interestingly, you can also do the following:
var Result = from Book in DB.BookResources
where Book.Title.Contains("java")
select Book;
Read More |
| When Linq and ref Parameters Meet | Posted on Tuesday, April 01, 2008 at 7:17 AM | It's fair to say that I'm a pretty heavy user of Linq. You'll find uses of it scattered across my code, from the obvious (using DLinq to query a database) to the slightly more exotic (writing queries over collections obtained from classes in System.Reflection). Linq often allows you to express a problem very neatly, resulting in compact, readable code. It also factors out the application of operations and leaves you to worry about the operations themselves, likely decreasing bugs.
Fixing A Bug
Today I ran across some code that took a parameter, then used it in a Linq query. Omitting the clutter, it looked something like this:
public void Lookup(string URL, ref int ID, ref int Status)
{
// ...stuff...
var Result = from D in DB.Datas
where D.URL == URL
select D;
// ...more stuff...
}
Read More |
| Very Quick Guide To DLinq: Part 2 | Posted on Tuesday, March 25, 2008 at 2:45 AM | In this part I'm going to take a look at inserts, updates and deletes using DLinq.
The Overall Model
First, to make changes to a database, you instantiate the data context object, just as we did before doing selections in the previous part.
var DB = new ResourcesDB();
You then do the required changes; we'll look at this part in just a moment. However, the inserts, updates and deletions are not performed immediately. They do not take place until you explicitly submit the changes:
DB.SubmitChanges();
This means that you can make many changes, then send them to the database all at once.
Inserting Data Into A Table
To make a single new entry to a database table, use the InsertOnSubmit method on the Table object. This takes a single parameter: an instance of the object that represents a row in the the table. You can use the object initializer syntax to write this very neatly.
DB.BookResources.InsertOnSubmit(new BookResource()
{
Read More |
| Very Quick Guide To DLinq: Part 1 | Posted on Friday, March 14, 2008 at 9:10 AM | We have already written about using Linq, but didn't cover DLinq. This post takes a quick look at getting started with DLinq, which allows you to write Linq queries against databases. Once you're up to speed, you can take a database and be writing Linq queries against it in under five minutes!
In future parts in this series of posts, we'll look at doing database updates, writing more complex queries and debugging. In this one, we'll just look at doing some simple queries.
Creating The Linq to SQL Classes
The bad news is that before you can use DLinq, you have to create Linq to SQL classes to represent your database tables. The good news is that Visual Studio 2008 (aside from the Express Edition) can do all of the hard work for you in this.
Read More |
| Do try...catch blocks hurt runtime performance? | Posted on Tuesday, February 19, 2008 at 6:23 AM | Recently, a friend mentioned that someone had told him that writing a try...catch block in C# (or substitute some other .Net language here) resulted in a "huge penalty" in terms of performance compared to if you had not written it. That is, merely writing such a block actually hurt program performance, even if an exception was never thrown. He didn't believe this was true, and rightly so - it's completely wrong. This post is a tidied up version of my explanation.
Inside a .Net Assembly
A .Net assembly, if we ignore various headers, consists of three things:
- Bytecode: a sequence of low-level instructions that specify the body of a method
- Metadata: a set of tables, a little bit like a database, describing higher level constructs such as classes, methods, signatures and so forth
- Heaps: places where string constants and other such things are stored
Read More |
| C#.NET or VB.NET? | Posted on Monday, February 11, 2008 at 9:10 AM | We get a lot of emails from Programmer's Heaven users. Amongst the requests that we do people's homework, which we ignore, we also get some more interesting questions sent to us too. We figured that when we do, we may as well share our answers more widely, and also this gives other folks a chance to give their input too. Recently we got one from someone who had discovered us through the C# School e-book and asked, "please kindly advise which of the two, C# and VB .NET is better for developing commercial applications like Payroll".
The Similarities
Both languages run in the Common Language Runtime and allow you to have full access to the .Net Class Library. This means that pretty much whatever is in the .Net Class Library is equally available to both VB.NET and C#.NET. Both languages have a similar feature set too, providing strong support for Object Oriented Programming, which is good for developing database-backed applications, as your Payroll surely will be.
Read More |
| Divvy up my list? Aye! | Posted on Thursday, January 31, 2008 at 4:28 AM | I wasn't born in Yorkshire, but I spent most of my childhood growing up there. Yorkshire, a large county in the north east of England, has its own collection of slang, not least including the word "aye", which it appears is appropriate in response to pretty much any situation. Another slang phrase is "divvy up". Basically, this just means to divide a bunch of things up somehow.
These days, I spend a lot of my time writing C#. If I'm dealing with a bunch of things in C#, I'll usually store them in a generic List collection, or some other collection. This got me thinking: how can I divvy up a List in C# into multiple Lists? And that's when I decided to implement the Divvy extension method.
What we're aiming for
Suppose we have a list of scores:
var Scores = new List<int>()
{ 87, 32, 45, 60, 91, 10, 58, 77, 66, 71 };
Read More |
| Higher Order Programming Is Easy! | Posted on Tuesday, January 22, 2008 at 7:09 AM | Higher Order Programming is one of those things that to many people sounds weird, magical, mysterious or just too hard for them to be able to do. It's not. If you have ever passed a function or method as a parameter to another function or method, then you have done higher order programming. If you have ever used a function pointer in C or a delegate in C# or some kind of callback mechanism, then you have done higher order programming.
Define it!
We are all used to writing code that takes parameters. We do it whenever we write subroutines, functions or methods. Normally these parameters are data. In higher order programming, one or more of these parameters is code rather than data. That is, you can pass code, functions and/or methods around, just as you can pass data around. That's all there is to it.
Show me an example
Let's look at C#, because the syntax will not be too unusual to programmers in many languages. Suppose we have a List of scores for a test.
Read More |
| C# 3.0 Part 4: Linq | Posted on Wednesday, December 12, 2007 at 5:12 AM | The final part of our C# 3.0 series is about Linq - Language Integrated Query.
Learn:
- What declarative programming is
- How to write Linq queries
- How to use ordering, joins, groups and query continuations
- What DLinq and XLinq are
Read it now!
|
| C# 3.0 Part 3: Initializers & Anonymous Types | Posted on Thursday, December 06, 2007 at 1:27 AM | The third article in our C# 3.0 series covers the new features that will help you to build data structures with less and more readable code.
The article covers:
- Object initializers
- Collection initializers
- Anonymous type is
- Type equivalence of anonymous types
Check it out.
|
| C# 3.0 Part 2: Extension Methods & Lambdas | Posted on Tuesday, November 27, 2007 at 9:02 AM | The second part of our C# 3.0 series is here, covering extension methods and lambda expressions. Learn:
- What an extension method is
- How to write extension methods
- The new lambda expression syntax
- To use the higher order programming paradigm
Go to the article! |
| Learn C# 3.0 With Our New Series! | Posted on Wednesday, November 21, 2007 at 9:31 AM | This week we are starting a brand new four part series, covering the new features of C# 3.0! The first article of the series covers type inference. You'll learn:
- What is type inference anyway?
- How can you take advantage of it?
- What does the new "var" keyword do?
- Is this at all related to Variant types in Visual Basic?
- Will using this hurt the performance of your programs at runtime?
Check out the article! |
| Understanding Casting and Coercion in C# | Posted on Saturday, October 20, 2007 at 2:32 PM | A little while back I answered a post on the PH forums about why you might need to use casts, and particularly why you might need to upcast. This article is a tidied up and extended version of my answer.
Variable vs. value types
Suppose we write a class B that inherits from class A. We can then say that B is a subclass of A. That means that you can use an object of type B anywhere that you can use an object of type A.
To understand upcasting and downcasting better, we need to introduce the idea of container or variable types and contrast them to value types. A variable may contain a value. For reference types, the variable has a type that states what type of value it can contain. However, the type of the value may be different; namely, it may be a subtype of the variable type. Let's clarify this with some examples.
A foo = new A(); // Variable type A, value type A
B bar = new B(); // Variable type B, value type B
A baz = new B(); // Variable type A, value type B
Read More |
|
Subscribe
RSS Feed
By Tag
.NET Algorithm alpha five C# C# 3.0 DLinq Higher Order Programming Java Lambda Expressions Linq Linux MySQL Performance Perl Programming SSH Test Driven Develoment Testing Threading UNIX
By Month
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
Help
Check out the Blog FAQ.
|