This example creates a Data Context class that creates a Customer property that can be used for querying. This example also shows how Attribute Mapping can be used to allow the entity class property names to be different from the table field names.

using System;
using System.Collections.Generic;
using System.Linq;
using LinqToVfp;
using IQToolkit.Data.Mapping;
using IQToolkit;

namespace LinqToVfpQuickStart {
    static class Program {
        [STAThread]
        static void Main() {
            string northwindDbcPath = "your path";
            string connectionString = string.Format("Provider=VFPOLEDB.1;Data Source={0};Exclusive=false;Ansi=true;", northwindDbcPath);

            Northwind northwind = new Northwind(connectionString);

            List<Customer> customers = (from customer in northwind.Customers
                                        orderby customer.Name
                                        select customer).ToList();
        }
    }

    // Data Context Class
    public class Northwind {
        private VfpQueryProvider provider;

        public Northwind(string connectionString) {
            this.provider = VfpQueryProvider.Create(connectionString, typeof(NorthwindAttributes).FullName);
        }

        public virtual IEntityTable<Customer> Customers {
            get { return this.provider.GetTable<Customer>("Customers"); }
        }
    }

    // Attribute Mapping Class
    public class NorthwindAttributes : Northwind {
        public NorthwindAttributes(string connectionString)
            : base(connectionString) {
        }

        [Table]
        [Column(Member = "Name", Name = "companyname")]
        [Column(Member = "Id", Name = "customerid")]
        public override IEntityTable<Customer> Customers {
            get { return base.Customers; }
        }
    }

    // Entity Class
    public class Customer {
        public string Id { get; set; }
        public string Name { get; set; }
    }
}

Last edited Apr 17, 2010 at 3:10 PM by TomBrothers, version 1

Comments

No comments yet.