Linq Asp.net Code Optimization

I use AdventureWorks as a database to familiarize myself with the linq and asp.net interface and was wondering if anyone could take a look at my C # code and give me an optimized way to get the results populated by the search and then updated when they are sent .

protected void btnSearch_Click(object sender, EventArgs e)
    {
        TestDataClassDataContext dc = new TestDataClassDataContext();
        Individual ind = new Individual();
        var q = from Individual in dc.Individuals
                where Individual.CustomerID == Convert.ToInt32(txtCustID.Text)
                select Individual;
        if (q.Count() > 0)
        {
            ind = q.First();
            Contact con = new Contact();
            var q2 = from Contact in dc.Contacts
                     where Contact.ContactID == ind.ContactID
                     select Contact;
            if (q2.Count() > 0)
            {
                con = q2.First();
                txtFname.Text = con.FirstName;
                txtLname.Text = con.LastName;
                txtMname.Text = con.MiddleName;
                txtPhone.Text = con.Phone;
                txtPword.Text = con.PasswordSalt;
                txtSuff.Text = con.Suffix;
                txtTitle.Text = con.Title;
                txtEmail.Text = con.EmailAddress;
                ddlEmailPromo.SelectedValue = con.EmailPromotion.ToString();

            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        TestDataClassDataContext dc = new TestDataClassDataContext();
        Individual ind = new Individual();
        var q = from Individual in dc.Individuals
                where Individual.CustomerID == Convert.ToInt32(txtCustID.Text)
                select Individual;
        if (q.Count() > 0)
        {
            ind = q.First();
            Contact con = new Contact();
            var q2 = from Contact in dc.Contacts
                     where Contact.ContactID == ind.ContactID
                     select Contact;
            if (q2.Count() > 0)
            {
                con = q2.First();
                con.FirstName = txtFname.Text;
                con.LastName = txtLname.Text;
                con.MiddleName = txtMname.Text;
                con.Phone = txtPhone.Text;
                con.PasswordSalt = txtPword.Text;
                con.Suffix = txtSuff.Text;
                con.Title = txtTitle.Text;
                con.EmailAddress = txtEmail.Text ;
                ddlEmailPromo.SelectedValue = con.EmailPromotion.ToString();

            }
        }
+3
source share
5 answers

In one request:

TestDataClassDataContext dc = new TestDataClassDataContext();
var con = (from individual in dc.Individuals 
                 join contact in dc.Contacts on individual.ContactID equals contact .ContactID
                 where individual.CustomerID == Convert.ToInt32(txtCustID.Text)
                 select contact).FirstOrDefault();

if(con == null) return;

txtFname.Text = con.FirstName;
txtLname.Text = con.LastName;
txtMname.Text = con.MiddleName;
txtPhone.Text = con.Phone;
txtPword.Text = con.PasswordSalt;
txtSuff.Text = con.Suffix;
txtTitle.Text = con.Title;
txtEmail.Text = con.EmailAddress;
ddlEmailPromo.SelectedValue = con.EmailPromotion.ToString();

Use the same query in the change submit function!

+4
source

- - . , , btnSearch_Click , .net , , :

dc.ObjectTrackingEnabled = false;

DataContext .net, , . : http://www.sidarok.com/web/blog/content/2008/05/02/10-tips-to-improve-your-linq-to-sql-application-performance.html

+1

SQL, DBML, SQL- .

:

TestDataClassDataContext dc = new TestDataClassDataContext();
Individual individual = dc.Individuals.Where(a => a.CustomerID == Convert.ToInt32(txtCustID.Text).FirstOrDefault();

if(individual != null)
{
   Contact contact = individual.Contacts.FirstOrDefault();
   if(contact != null)
   {
       txtFname.Text = contact.FirstName;
   }
}
0

1) asp.net , .

2) web.config alsow .

3) ( , ) .

var q = from Individual in dc.Individuals
                where Individual.CustomerID == Convert.ToInt32(txtCustID.Text)
                select Individual;

q DOES NOT contain any objects - this is just a request. Now let's count how many times it runs:

1. if (q.Count() > 0)

  1. ind = q.First();

  2. q2.Count() > 0

At least 3 times per request, your connection is established, open and closed. It takes time. Try to use your resources sparingly.

Hope this helps, Ilya.

0
source

Also note that each call to .Count () will list the full set of results to return the score.

What you are looking for is the .Any () method, which will work in constant time.

0
source

Source: https://habr.com/ru/post/1736367/


All Articles