Solved

more efficent LINQ statement + how to ignore if a string is empty without writing a load of if statements

Posted on 2011-02-23
9
364 Views
Last Modified: 2012-05-11
hi all,

my code below should explain it all.

its a function to insert and or update data in my company table
is there a more efficent way to do this, if so can someone show me?

also in the update section, is there a method that will ignore updating a field if the string is blank, or do i have to do an if statement for eachone? :S

Thanks
public static void InsertOrUpdateCompanyInfo(string StrID, string StrACK,
            string StrRMID, string StrUser, string StrSec1, string StrSec2, string StrSec3, string StrSec4, string StrSec5,
            string StrCompanyType, string StrOfficeTel, string StrOfficeFax, string StrTrades, string StrCompanyReg,
            string StrCompanyName, string StrAddress1, string StrAddress2, string StrAddress3, string StrPostcode,
            string StrWebsite, string StrYearEstablished, string StrConstructionline, string StrCHAS, string StrNotes,
            string StrLocked)
        {
            using (MiscDataContext dc = new MiscDataContext())
            {
                Table<CompanyInfo> CompanyData = dc.GetTable<CompanyInfo>();
                if (StrID == "")
                {
                    CompanyInfo CData = new CompanyInfo();
                    CData.ACK = Convert.ToInt16(StrACK);
                    CData.RMID = Convert.ToInt16(StrRMID);
                    CData.Sec1 = Convert.ToInt16(StrSec1);
                    CData.Sec2 = Convert.ToInt16(StrSec2);
                    CData.Sec3 = Convert.ToInt16(StrSec3);
                    CData.Sec4 = Convert.ToInt16(StrSec4);
                    CData.Sec5 = Convert.ToInt16(StrSec5);                  
                    CData.CompanyType = StrCompanyType;
                    CData.OfficeTel = StrOfficeTel;
                    CData.OfficeFax = StrOfficeFax;
                    CData.Trades = StrTrades;
                    CData.CompanyReg =StrCompanyReg;
                    CData.CompanyName = StrCompanyName;
                    CData.Address1 = StrAddress1;
                    CData.Address2 = StrAddress2;
                    CData.Address3 = StrAddress3;
                    CData.Postcode = StrPostcode;
                    CData.Website = StrWebsite;
                    CData.YearEstablished = StrYearEstablished;
                    CData.Constructionline = StrConstructionline;
                    CData.CHAS = StrCHAS;
                    CData.Notes = StrNotes;
                    CData.Locked = Convert.ToInt16(StrLocked);
                    CData.DateAdded = DateTime.Now;

                    CompanyData.InsertOnSubmit(CData);
                }
                else
                {
                    int NewStrID = Convert.ToInt16(StrID);
                    CompanyInfo MatchedCompany = CompanyData.SingleOrDefault(c => c.ID == NewStrID);
                    MatchedCompany.ACK = Convert.ToInt16(StrACK);
                    MatchedCompany.RMID = Convert.ToInt16(StrRMID);
                    MatchedCompany.Sec1 = Convert.ToInt16(StrSec1);
                    MatchedCompany.Sec2 = Convert.ToInt16(StrSec2);
                    MatchedCompany.Sec3 = Convert.ToInt16(StrSec3);
                    MatchedCompany.Sec4 = Convert.ToInt16(StrSec4);
                    MatchedCompany.Sec5 = Convert.ToInt16(StrSec5);
                    MatchedCompany.CompanyType = StrCompanyType;
                    MatchedCompany.OfficeTel = StrOfficeTel;
                    MatchedCompany.OfficeFax = StrOfficeFax;
                    MatchedCompany.Trades = StrTrades;
                    MatchedCompany.CompanyReg = StrCompanyReg;
                    MatchedCompany.CompanyName = StrCompanyName;
                    MatchedCompany.Address1 = StrAddress1;
                    MatchedCompany.Address2 = StrAddress2;
                    MatchedCompany.Address3 = StrAddress3;
                    MatchedCompany.Postcode = StrPostcode;
                    MatchedCompany.Website = StrWebsite;
                    MatchedCompany.YearEstablished = StrYearEstablished;
                    MatchedCompany.Constructionline = StrConstructionline;
                    MatchedCompany.CHAS = StrCHAS;
                    MatchedCompany.Notes = StrNotes;
                    MatchedCompany.Locked = Convert.ToInt16(StrLocked);
                    MatchedCompany.DateEdited = DateTime.Now;
                    MatchedCompany.EditedBy = StrUser;
                }
                dc.SubmitChanges();
            }
        }

Open in new window

0
Comment
Question by:awilderbeast
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34969403
Are you talking about the Lambda expression here?

    CompanyInfo MatchedCompany = CompanyData.SingleOrDefault(c => c.ID == NewStrID);
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 34969425
The attached code snippets should make the code more maintainable.  I doubt that it will be much (if any) more efficient.

The second would be a method to update a string only if it is non-null, you would use it like this:
UpdateString(ref CData.Address1, StrAddress1);

Open in new window


You could add code so that if the string is "", then set the address to NULL.
public static void InsertOrUpdateCompanyInfo(string StrID, string StrACK,
            string StrRMID, string StrUser, string StrSec1, string StrSec2, string StrSec3, string StrSec4, string StrSec5,
            string StrCompanyType, string StrOfficeTel, string StrOfficeFax, string StrTrades, string StrCompanyReg,
            string StrCompanyName, string StrAddress1, string StrAddress2, string StrAddress3, string StrPostcode,
            string StrWebsite, string StrYearEstablished, string StrConstructionline, string StrCHAS, string StrNotes,
            string StrLocked)
{
	using (MiscDataContext dc = new MiscDataContext())
	{
		Table<CompanyInfo> CompanyData = dc.GetTable<CompanyInfo>();
		CompanyInfo CData;
		if (StrID == "")
		{
			CData = new CompanyInfo();
			CData.DateAdded = DateTime.Now;

			CompanyData.InsertOnSubmit(CData);
		}
		else
		{
			int NewStrID = Convert.ToInt16(StrID);
			CData = CompanyData.SingleOrDefault(c => c.ID == NewStrID);
			CData.DateEdited = DateTime.Now;
			CData.EditedBy = StrUser;
		}
		CData.ACK = Convert.ToInt16(StrACK);
		CData.RMID = Convert.ToInt16(StrRMID);
		CData.Sec1 = Convert.ToInt16(StrSec1);
		CData.Sec2 = Convert.ToInt16(StrSec2);
		CData.Sec3 = Convert.ToInt16(StrSec3);
		CData.Sec4 = Convert.ToInt16(StrSec4);
		CData.Sec5 = Convert.ToInt16(StrSec5);                  
		CData.CompanyType = StrCompanyType;
		CData.OfficeTel = StrOfficeTel;
		CData.OfficeFax = StrOfficeFax;
		CData.Trades = StrTrades;
		CData.CompanyReg =StrCompanyReg;
		CData.CompanyName = StrCompanyName;
		CData.Address1 = StrAddress1;
		CData.Address2 = StrAddress2;
		CData.Address3 = StrAddress3;
		CData.Postcode = StrPostcode;
		CData.Website = StrWebsite;
		CData.YearEstablished = StrYearEstablished;
		CData.Constructionline = StrConstructionline;
		CData.CHAS = StrCHAS;
		CData.Notes = StrNotes;
		CData.Locked = Convert.ToInt16(StrLocked);

		dc.SubmitChanges();
	}
}

Open in new window

///<Summary>
///  Update the 'UpdateString' only if newValue is not NULL
///</Summary>
public void UpdateIfChanged(ref string UpdateString, string newValue)
{
    if (newValue!=null)
       UpdateString=newValue;    
}

Open in new window

0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34969457
thanks i can see how the first bit of coding is alot better and i understand it too :)

just the second bit of code i dont get
how do i use that and where?

Thanks
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 21

Expert Comment

by:MogalManic
ID: 34970201
Instead of doing this
           
cData.Address1 = StrAddress1;

Open in new window

you would call the updateIfChanged method:
           
UpdateIfChanged(ref cData.Address1, StrAddress1);

Open in new window

0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34970300
so if i changed all the

CData.Name = StrName

to

UpdateIfChanged(ref cData.Name, StrName);

would it still work for inserting a new record in the same function?

Thanks

0
 
LVL 21

Expert Comment

by:MogalManic
ID: 34970394
It will work for any entity class for insert or update.

You could put the method in a static "Utils" class so that it is something like this:
/// <summary>
/// Utility class for managing entity objects
/// </summary>
public static class EntityUtils
{
    /// <summary>
    /// Update the 'UpdateString' only if newValue is not NULL.
    /// If newValue is "", then set the UpdateString to NULL.
    /// (there will be no way to set the UpdateString to "")
    /// </summary>
    /// <param name="UpdateString">The update string.</param>
    /// <param name="newValue">The new value.</param>
    public static void UpdateIfChanged(ref string UpdateString, string newValue)
    {
        if (newValue != null)
            UpdateString = newValue;
        if (newValue.Length == 0)
            UpdateString = null;
    }
}

Open in new window

//Then the code would be :

EntityUtils.UpdateIfChanged(ref cData.Name, StrName);

Open in new window

0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34970581
sorry im quite new to C# and still learning

heres what im doing, tell me if im way off

the line
EntityUtils.UpdateIfChanged(ref CData.ACK, Convert.ToInt16(StrACK));

is giving out:
overloaded method string string invalid arguments, am i using it right, or can the EntityUtils only accept strings at the moment?

tried this
EntityUtils.UpdateIfChanged(ref CData.CompanyName, StrCompanyName);
gives out:
a property or index member may not be passed as an out or ref parameter

thanks for your patience

public static class EntityUtils
        {
            /// <summary>
            /// Update the 'UpdateString' only if newValue is not NULL.
            /// If newValue is "", then set the UpdateString to NULL.
            /// (there will be no way to set the UpdateString to "")
            /// </summary>
            /// <param name="UpdateString">The update string.</param>
            /// <param name="newValue">The new value.</param>
            public static void UpdateIfChanged(ref string UpdateString, string newValue)
            {
                if (newValue != null)
                    UpdateString = newValue;
                if (newValue.Length == 0)
                    UpdateString = null;
            }
        }
        public static void InsertOrUpdateCompanyInfo(string StrID, string StrACK,
                    string StrRMID, string StrUser, string StrSec1, string StrSec2, string StrSec3, string StrSec4, string StrSec5,
                    string StrCompanyType, string StrOfficeTel, string StrOfficeFax, string StrTrades, string StrCompanyReg,
                    string StrCompanyName, string StrAddress1, string StrAddress2, string StrAddress3, string StrPostcode,
                    string StrWebsite, string StrYearEstablished, string StrConstructionline, string StrCHAS, string StrNotes,
                    string StrLocked)
        {
            using (MiscDataContext dc = new MiscDataContext())
            {
                Table<CompanyInfo> CompanyData = dc.GetTable<CompanyInfo>();
                CompanyInfo CData;
                if (StrID == "")
                {
                    CData = new CompanyInfo();
                    CData.DateAdded = DateTime.Now;
                    CompanyData.InsertOnSubmit(CData);
                }
                else
                {
                    int NewStrID = Convert.ToInt16(StrID);
                    CData = CompanyData.SingleOrDefault(c => c.ID == NewStrID);
                    CData.DateEdited = DateTime.Now;
                    CData.EditedBy = StrUser;
                }
                EntityUtils.UpdateIfChanged(ref CData.ACK, Convert.ToInt16(StrACK));
                EntityUtils.UpdateIfChanged(ref CData.CompanyName, StrCompanyName);

                CData.ACK = Convert.ToInt16(StrACK);
                CData.RMID = Convert.ToInt16(StrRMID);
                CData.Sec1 = Convert.ToInt16(StrSec1);
                CData.Sec2 = Convert.ToInt16(StrSec2);
                CData.Sec3 = Convert.ToInt16(StrSec3);
                CData.Sec4 = Convert.ToInt16(StrSec4);
                CData.Sec5 = Convert.ToInt16(StrSec5);
                CData.CompanyType = StrCompanyType;
                CData.OfficeTel = StrOfficeTel;
                CData.OfficeFax = StrOfficeFax;
                CData.Trades = StrTrades;
                CData.CompanyReg = StrCompanyReg;
                CData.CompanyName = StrCompanyName;
                CData.Address1 = StrAddress1;
                CData.Address2 = StrAddress2;
                CData.Address3 = StrAddress3;
                CData.Postcode = StrPostcode;
                CData.Website = StrWebsite;
                CData.YearEstablished = StrYearEstablished;
                CData.Constructionline = StrConstructionline;
                CData.CHAS = StrCHAS;
                CData.Notes = StrNotes;
                CData.Locked = Convert.ToInt16(StrLocked);

                dc.SubmitChanges();
            }
        }

Open in new window

0
 
LVL 1

Author Comment

by:awilderbeast
ID: 35006308
ok code below

and error in image
public void UpdateIfChanged(ref string UpdateString, string newValue)
        {
            if (newValue != null)
                UpdateString = newValue;
        }

        //COMPANY INFO
        public static void InsertOrUpdateCompanyInfo(string StrID, string StrACK,
            string StrRecordManager, string StrUser, string StrSec1, string StrSec2, string StrSec3, string StrSec4, string StrSec5,
            string StrCompanyType, string StrOfficeTel, string StrOfficeFax, string StrTrades, string StrCompanyReg,
            string StrCompanyName, string StrAddress1, string StrAddress2, string StrAddress3, string StrPostcode,
            string StrWebsite, string StrYearEstablished, string StrConstructionline, string StrCHAS, string StrNotes,
            string StrLocked)
        {
            using (MiscDataContext dc = new MiscDataContext())
            {
                Table<CompanyInfo> CompanyData = dc.GetTable<CompanyInfo>();
                CompanyInfo CData = new CompanyInfo();
                if (!String.IsNullOrEmpty(StrID))
                {
                    int NewStrID = Convert.ToInt16(StrID);
                    CData = CompanyData.SingleOrDefault(c => c.ID == NewStrID);
                    CData.EditedBy = StrUser;
                    CData.DateEdited = DateTime.Now;

                }
                else
                {
                    CData.RecordManager = StrUser;
                    CData.DateAdded = DateTime.Now;
                }
                UpdateIfChanged(ref CData.ACK, StrACK);

Open in new window

Untitled-1.png
0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 35019580
thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net web app 3 54
Starting to use Git with Visual  Studio Online 1 64
CSS question 16 57
What!!!??? 5 57
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question