Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用linq时防止重复代码_C#_Sql_Linq - Fatal编程技术网

C# 使用linq时防止重复代码

C# 使用linq时防止重复代码,c#,sql,linq,C#,Sql,Linq,我目前正在使用C#和linq在SQL数据库中插入/更新数据。下面的代码工作得很好,但我觉得它很凌乱,而且是重复的 请你看一下下面的代码,告诉我是否有一个更快的更新数据的方法,而不是我必须重复我的代码 多谢各位 Incident inc = new Incident { AccountID = AccountID, SiteID = siteID, DepartmentID = departmentID,

我目前正在使用C#和linq在SQL数据库中插入/更新数据。下面的代码工作得很好,但我觉得它很凌乱,而且是重复的

请你看一下下面的代码,告诉我是否有一个更快的更新数据的方法,而不是我必须重复我的代码

多谢各位

Incident inc = new Incident
        {
            AccountID = AccountID,
            SiteID = siteID,
            DepartmentID = departmentID,
            LocationID = LocationID,
            QuestionCategoryID = CategoryID,
            IncidentSourceID = IncidentSourceID,
            IncidentTypeID = IncidentTypeID,
            NonConformanceTypeID = NonConID,
            ProductGroupID = ProductGroupID,
            ProductID = ProductID,
            ComponentID = ComponentID,
            ProductReference = prodRef,
            CurrentAssignedUserID = UserId,
            CurrentAssignedContactID = contactid,
            OriginalAssignedUser = UserId,
            OriginalAssignedContact = contactid,
            LoggedByUserID = logUserId,
            LoggedByContactID = logContactid,
            IncidentTitleID = IncidentTitleID,
            Title = IncidentTitle.ToString(),
            Description = problemDesc,
            Comments = comments,
            ActionsRequired = actions,
            RiskPriorityID = RiskPriorityTypeID,
            AffectedPartyID = affectedPartyID,
            ImpactLevel = Convert.ToInt32(impact),
            Justification = justification,
            EsculationDate = DateTime.Today,
            PriorityID = PriorityID,
            OriginalPriorityID = PriorityID,
            CreatedByUser = Convert.ToInt32(loggedInUserID),
            UpdatedBy = Convert.ToString(loggedInUserID),
            RiskID = RiskID,
            Active = true,
            StatusID = 1,

            DelayedDate = null,
            IncidentCloseDate = null,
            IncidentDate = DateTime.Now,
            IncidentPendingDate = DateTime.Now,
            LoggedDate = DateTime.Now,
            LastUpdated = DateTime.Now,
            LastActionTaken = DateTime.Now
        };

        // Save the data to the database
        if (Request.QueryString["IncidentID"] == null)
        {
            // Insert a new incident.
            db.Incidents.Add(inc);
            db.SaveChanges();

        }
        else
        {
            //update an existing incident.
            long ID = Convert.ToInt64(Request.QueryString["IncidentID"]);
            var record = db.Incidents.Where(i => i.IncidentID == ID).FirstOrDefault();

            record.AccountID = AccountID;
            record.SiteID = siteID;
            record.DepartmentID = departmentID;
            record.LocationID = LocationID;
            record.QuestionCategoryID = CategoryID;
            record.IncidentSourceID = IncidentSourceID;
            record.IncidentTypeID = IncidentTypeID;
            record.NonConformanceTypeID = NonConID;
            record.ProductGroupID = ProductGroupID;
            record.ProductID = ProductID;
            record.ComponentID = ComponentID;
            record.ProductReference = prodRef;
            record.CurrentAssignedUserID = UserId;
            record.CurrentAssignedContactID = contactid;
            record.OriginalAssignedUser = UserId;
            record.OriginalAssignedContact = contactid;
            record.LoggedByUserID = logUserId;
            record.LoggedByContactID = logContactid;
            record.IncidentTitleID = IncidentTitleID;
            record.Title = IncidentTitle.ToString();
            record.Description = problemDesc;
            record.Comments = comments;
            record.ActionsRequired = actions;
            record.RiskPriorityID = RiskPriorityTypeID;
            record.AffectedPartyID = affectedPartyID;
            record.ImpactLevel = Convert.ToInt32(impact);
            record.Justification = justification;
            record.EsculationDate = DateTime.Today;
            record.PriorityID = PriorityID;
            record.OriginalPriorityID = PriorityID;
            record.CreatedByUser = Convert.ToInt32(loggedInUserID);
            record.UpdatedBy = Convert.ToString(loggedInUserID);
            record.RiskID = RiskID;
            record.Active = true;
            record.StatusID = 1;

            record.DelayedDate = null;
            record.IncidentCloseDate = null;
            record.IncidentDate = DateTime.Now;
            record.IncidentPendingDate = DateTime.Now;
            record.LoggedDate = DateTime.Now;
            record.LastUpdated = DateTime.Now;
            record.LastActionTaken = DateTime.Now;

            db.SaveChanges();
        }

只需将属性设置一次,不管它是否为现有实体

var id = Request.QueryString["IncidentID"];
var incidentId = String.IsNullOrEmpty(id) ? 0 : int.Parse(id);

var record = incidentId  !=0 ? 
   db.Incidents.FirstOrDefault(i => i.IncidentID == incidentId); : new Incident();

record.AccountID = AccountID;
record.SiteID = siteID;
record.DepartmentID = departmentID;
//etc.......

if (incidentId == 0)
{
     //set any fields here that are for add only
     record.CreatedByUser = ...
     db.Incidents.Add(record);
}
db.SaveChanges();

您可以执行以下操作:

var incidentInitializer = new Action<Incident>(incident =>
{
  incident.AccountID = AccountID,
  incident.SiteID = siteID,
  ...
};

// Save the data to the database
if (Request.QueryString["IncidentID"] == null)
{
    // Insert a new incident.
    var inc = new Incident();
    incidentInitializer(inc);

    db.Incidents.Add(inc);
    db.SaveChanges();
}
else
{
    //update an existing incident.
    long ID = Convert.ToInt64(Request.QueryString["IncidentID"]);
    var record = db.Incidents.Where(i => i.IncidentID == ID).FirstOrDefault();
    incidentInitializer(record);

    db.SaveChanges();
}
var incidentializer=新操作(事件=>
{
incident.AccountID=AccountID,
incident.SiteID=SiteID,
...
};
//将数据保存到数据库中
if(Request.QueryString[“IncidentID”]==null)
{
//插入新事件。
var inc=新事件();
Incidentializer(公司);
db.Incidents.Add(inc);
db.SaveChanges();
}
其他的
{
//更新现有事件。
long ID=Convert.ToInt64(Request.QueryString[“IncidentID”]);
var record=db.incents.Where(i=>i.IncidentID==ID).FirstOrDefault();
入职初始化器(记录);
db.SaveChanges();
}

也许应该在以下位置询问此问题。
var incidentInitializer = new Action<Incident>(incident =>
{
  incident.AccountID = AccountID,
  incident.SiteID = siteID,
  ...
};

// Save the data to the database
if (Request.QueryString["IncidentID"] == null)
{
    // Insert a new incident.
    var inc = new Incident();
    incidentInitializer(inc);

    db.Incidents.Add(inc);
    db.SaveChanges();
}
else
{
    //update an existing incident.
    long ID = Convert.ToInt64(Request.QueryString["IncidentID"]);
    var record = db.Incidents.Where(i => i.IncidentID == ID).FirstOrDefault();
    incidentInitializer(record);

    db.SaveChanges();
}