Tuesday, June 16, 2009

Search and selection of data using LINQ

In our programming practice we often face a situation, when we should search and select some data according to parameters, which are defined by users. In this case we, as usual, have some form (screen) with special fields, which allow to a user to select parameters for his own choice. For example, we have some form to view data of a table (named Persons) with fields ID, FirstName, LastName, (some other fields), HomeSite. Our users want to find in one case only person with HomeSite London1 and FirstName Mike; in another case a user want to see all persons, that there are in our table. In this article I will show how you can use LINQ for such situations.

For simplicity we will consider a little project with only two tables: C_Person and NC_Site (fig.1) :

01.GIF

Fig. 1.

In our solution (named So) there are three projects: project with all our web forms etc. (So), project for our business logic (SoBL, output type class library) and project for data access layers (SoDal, output type class library). In SoDal project there is dbml file (LINQ to SQL class), named SoDC.dbml, with our tables (fig. 2):

See full detail: http://www.c-sharpcorner.com/UploadFile/LivMic/CaseSelectLinq05262009000059AM/CaseSelectLinq.aspx

No comments: