devxlogo

Using LINQ for Queries

Using LINQ for Queries

LINQ?is?very?good?and?efficient?when?it?comes?to?handling?data?coming?from?a?datasource.?This?little?example?creates?a?Table?object.?Then?creates?a?DataContext?to?interpret?the?Table?data,?and?finally?does?a?query?to?extract?the?wanted?data.
????????[Table(Name?=?”Courses”)]
????????public?class?Courses?//Create?Courses?Table
????????{
????????????[Column(IsPrimaryKey?=?true,?DbType?=?”BIGINT?NOT?NULL?IDENTITY”,?IsDbGenerated?=?true)]
????????????public?Int32?CourseID;?//Field
????????????[Column]
????????????public?string?CourseDesc;?//Field
????????}
????????????????using?(DataContext?dcCourses?=?new?DataContext(“CONNECTION_STRING))?//Connect?to?DB
????????????????{
????????????????????var?data?=?(from?c?in?_Courses
????????????????????????????????where?c.CourseID?==?ComboBox1.SelectedIndex
????????????????????????????????select?new?{?c.CourseDesc?}
????????????????????).FirstOrDefault();?//Select?CourseID?That?Corresponds?to?ComboBox?Selection
????????????????????TextBox1.Text?=?data.Name;?//Display
?????????????????}

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist