Click here to Skip to main content
12,066,448 members (57,182 online)
Click here to Skip to main content

Stats

27.9K views
1.5K downloads
74 bookmarked
Posted

LINQ FAQ: Part 3

, 28 Jul 2009 CPOL
Concurrency handling, compiled queries, CRUD implementation, and mapping simple .NET classes with XML file configuration.
LINQConcurrency.zip
LINQConcurrency
LINQConcurrency
App_Data
bin
LINQConcurrency.dll
LINQConcurrency.csproj.user
Properties
LINQCrudWithEntities.zip
LINQCrudWithEntities
LINQCrudWithEntities
App_Data
Sales.mdf
Sales_log.LDF
bin
LINQCrudWithEntities.dll
LINQtoSQLStoredProc.dll
LINQCrudWithEntities.csproj.user
Properties
LINQCrudWithEntities
App_Data
Sales.mdf
Sales_log.LDF
bin
LINQCrudWithEntities.dll
LINQtoSQLStoredProc.dll
LINQCrudWithEntities.csproj.user
Properties
LINQPerformance.zip
LINQPerformance
Send
Book1.xlsx
LINQtoSQL
SimpleLinqExample
App_Data
bin
SimpleLinqExample.dll
obj
Debug
TempPE
Properties
SimpleLinqExample.csproj.user
SQL
SimpleLINQExample
SimpleLINQExample
WindowFormLINQ
Country.mdf
Country_log.LDF
WindowFormLINQ
bin
Debug
WindowFormLINQ.exe
WindowFormLINQ.vshost.exe
WindowFormLINQ.vshost.exe.manifest
Release
WindowFormLINQ.exe
obj
Debug
Release
Properties
Settings.settings
WindowFormLINQ.csproj.user
WebAppMappingXML.zip
WebAppMappingXML
WebAppMappingXML
App_Data
bin
WebAppMappingXML.dll
obj
Debug
TempPE
Properties
WebAppMappingXML.csproj.user
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace WindowFormLINQ
{
    
    public partial class frmSimpleLINQExample : Form
    {
        public frmSimpleLINQExample()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void btnLoadFromCustomObject_Click(object sender, EventArgs e)
        {
           

           clsCustomer[] Customers = new clsCustomer[] { new clsCustomer { CustomerName = "Raju", CustomerCode = "001" }, new clsCustomer { CustomerName = "Shiv", CustomerCode = "002" } };

            var query = from c in Customers
                        where c.CustomerCode == txtCustomerCode.Text
                        select new { c.CustomerCode, c.CustomerName };

            var enumerator = query.GetEnumerator();
            
            lstCountry.Items.Clear();
            
            if (enumerator.MoveNext())
            {
                var customer = enumerator.Current;
                lstCountry.Items.Add(customer.CustomerName);
            }
        }

        private void btnLoadFromDatabase_Click(object sender, EventArgs e)
        {
            DataContext db = new DataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\SimpleLINQExample\WindowFormLINQ\Country.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            Table<clsCustomer> Customers = db.GetTable<clsCustomer>();

            var query = from c in Customers
                        where c.CustomerCode == txtCustomerCode.Text
                        select new { c.CustomerCode, c.CustomerName };

            var enumerator = query.GetEnumerator();
            lstCountry.Items.Clear();
            if (enumerator.MoveNext())
            {
                var customer = enumerator.Current;
                lstCountry.Items.Add(customer.CustomerName);
            }
        }
    }
    [Table(Name="Customer")]
    public class clsCustomer
    {
        private string _strCustomerName;
        private string _strCustomerCode;
        [Column(Name="CustomerName")]
        public string CustomerName
        {
            set
            {
                _strCustomerName = value;
            }
            get
            {
                return _strCustomerName;
            }
        }
        [Column(Name = "CustomerCode")]
        public string CustomerCode
        {
            set
            {
                _strCustomerCode = value;
            }
            get
            {
                return _strCustomerCode;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160208.1 | Last Updated 28 Jul 2009
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid