?
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
Medium Priority
?
371 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 1500 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 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