none
How to map a Database Table from a DataContext for a Linq Query? RRS feed

  • Question

  • My Employee database contains a Table called "Person".  This "Person" table contains a column called "Title".  I want to create a Linq query on the "Person" table and query the "Title" column.  I think I need to map the "Person" table in my DataContext, but I don't know how.  Could someone explain how this is done?  Or how I could query the "Person" table in this scenario?

    Using System
    //...
    Using System.Data.Linq
    //...
    
    string conn1 = "Data Source=WKS308\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True;";
    DataContext peepS =  new DataContext(conn1);
    
    var query = from x in peepS.Person
                select new { x.Title };
    Thanks

    Rich P



    • Edited by Rich P123 Thursday, October 17, 2013 6:52 PM ...
    Thursday, October 17, 2013 6:51 PM

Answers

  • Thank you for your reply.  I actually did come up with something (after an Alligator style wrestling match with Google and Bing) that works:

    using System.Data.Linq;
    //...
    public void MyProc()
    {
       string conn1 = "Data Source=WKS308\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True;";
       DataContext peepS =  new DataContext(conn1);
       Table<Peepers> People = peepS.GetTable<Peepers>();
       var qry = from peep in People
           where peep.BusinessEntityID < 11
           select peep;
       Array.ForEach(qry.ToArray(), p => Console.WriteLine(p.FirstName));
    }
    
    //-------------------------------
    
    [Table(Name = "Person")]
    public class Peepers
    {
        [Column()]
        public int BusinessEntityID { get; set; }
        [Column()]
        public string PersonType { get; set; }
        [Column()]
        public bool NameStyle{get;set;}
        [Column()]
        public string Title { get; set; }
        [Column()]
        public string FirstName { get; set; }
        [Column()]
        public string MiddleName { get; set; }
        [Column()]
        public string LastName { get; set; }
        [Column()]
        public string Suffix { get; set; }
        [Column()]
        public int EmailPromotion { get; set; }
        [Column()]
        public DateTime ModifiedDate { get; set; }
    }


    Rich P


    • Edited by Rich P123 Thursday, October 17, 2013 11:21 PM ....
    • Marked as answer by Fred BaoModerator Thursday, October 24, 2013 9:17 AM
    Thursday, October 17, 2013 11:06 PM

All replies

  • Hi Rich;

    The query looks OK. Although in this case you can form the quay as follows because there is only one column specified in the select statement.

    var query = from x in peepS.Person
                       select x.Title;

    Please post any exception and inner exception messages you may be getting.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, October 17, 2013 8:53 PM
  • Thank you for your reply.  I actually did come up with something (after an Alligator style wrestling match with Google and Bing) that works:

    using System.Data.Linq;
    //...
    public void MyProc()
    {
       string conn1 = "Data Source=WKS308\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True;";
       DataContext peepS =  new DataContext(conn1);
       Table<Peepers> People = peepS.GetTable<Peepers>();
       var qry = from peep in People
           where peep.BusinessEntityID < 11
           select peep;
       Array.ForEach(qry.ToArray(), p => Console.WriteLine(p.FirstName));
    }
    
    //-------------------------------
    
    [Table(Name = "Person")]
    public class Peepers
    {
        [Column()]
        public int BusinessEntityID { get; set; }
        [Column()]
        public string PersonType { get; set; }
        [Column()]
        public bool NameStyle{get;set;}
        [Column()]
        public string Title { get; set; }
        [Column()]
        public string FirstName { get; set; }
        [Column()]
        public string MiddleName { get; set; }
        [Column()]
        public string LastName { get; set; }
        [Column()]
        public string Suffix { get; set; }
        [Column()]
        public int EmailPromotion { get; set; }
        [Column()]
        public DateTime ModifiedDate { get; set; }
    }


    Rich P


    • Edited by Rich P123 Thursday, October 17, 2013 11:21 PM ....
    • Marked as answer by Fred BaoModerator Thursday, October 24, 2013 9:17 AM
    Thursday, October 17, 2013 11:06 PM