You??™ll recall that the code in the earlier example simply queried the DirectoryInfo
class to return the directories on your local C drive. Combining it with this query, you join the Name
property of the DirectoryInfo class to the DirectoryName column from the DirectoryInformation
9
Part I: Introduction to Project LINQ
table to return the DirectoryDescription information from the table. Just add the following highlighted
code to the earlier query:
DirectoryInfo di = new DirectoryInfo("C:\\");
var query =
from dir in di.GetDirectories()
orderby di.Name
select new
{
dir.Name,
DirectoryDescription = (
from d in db.DirectoryInformation
where d.DirectoryName == di.Name
select d.DirectoryDescription).FirstOrDefault()
};
foreach (var item in query)
listBox1.Items.Add(item.Name + " " + item.DirectoryDescription);
}
To run this example, you first need to create a table in a database. The example used the AdventureWorks
database and the following code to create the table:
CREATE TABLE [dbo].[DirectoryInformation](
[DirectoryName] [varchar](50) NULL,
[DirectoryDescription] [varchar](255) NULL
) ON PRIMARY
GO
You can use the following INSERT statement to add data to the DirectoryInformation table:
INSERT INTO DirectoryInformation (DirectoryName, DirectoryDescription)
VALUES (??™Windows??™, ??™My Windows Directory??™)
GO
Before continuing, think about the amount of code you would have had to write to accomplish the same
type of query in pre-LINQ technology.
Pages:
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46