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
?????????????????}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Related Posts