C# 是否有任何linqish方法来保存用户表单数据-重新分解我的代码
我有一个简单的表单,它获取用户数据并将其保存到数据库中。虽然它工作正常,但似乎很重复。。我不确定我是否能以某种方式转换代码,以检查用户输入并将其保存在DB中 这是我的密码C# 是否有任何linqish方法来保存用户表单数据-重新分解我的代码,c#,asp.net,linq,C#,Asp.net,Linq,我有一个简单的表单,它获取用户数据并将其保存到数据库中。虽然它工作正常,但似乎很重复。。我不确定我是否能以某种方式转换代码,以检查用户输入并将其保存在DB中 这是我的密码 public void SaveUserData() { MWCompetitionContestantsDetails user = new MWCompetitionContestantsDetails(); MWCompetitionsEntryDetails
public void SaveUserData()
{
MWCompetitionContestantsDetails user = new MWCompetitionContestantsDetails();
MWCompetitionsEntryDetails entry = new MWCompetitionsEntryDetails();
if (!IsNullOrEmpty(firstNameText.Value))
user.FirstName = firstNameText.Value;
if (!IsNullOrEmpty(lastNameText.Value))
user.LastName = lastNameText.Value;
if (!IsNullOrEmpty(emailText.Value))
user.UserEmailAddress = emailText.Value;
if (!IsNullOrEmpty(address1Text.Value))
user.Address1= address1Text.Value;
if (!IsNullOrEmpty(address2Text.Value))
user.Address2 = address2Text.Value;
if (!IsNullOrEmpty(cityText.Value))
user.City = cityText.Value;
if (!IsNullOrEmpty(provinceText.Value))
user.Province= provinceText.Value;
if (!IsNullOrEmpty(postCodeText.Value))
user.PostalCode = postCodeText.Value;
if (!IsNullOrEmpty(countryText.SelectedItem.Text))
user.Country = countryText.SelectedItem.Text;
user.Save();
}
public static bool IsNullOrEmpty<T>(T value)
{
if (typeof(T) == typeof(string))
return string.IsNullOrEmpty(value as string);
return value.Equals(default(T));
}
public void SaveUserData()
{
MWCompetitionContentsDetails用户=新的MWCompetitionContentsDetails();
MWCompetitionEntryDetails条目=新的MWCompetitionEntryDetails();
如果(!IsNullOrEmpty(firstNameText.Value))
user.FirstName=firstNameText.Value;
如果(!IsNullOrEmpty(lastNameText.Value))
user.LastName=lastNameText.Value;
如果(!IsNullOrEmpty(emailText.Value))
user.UserEmailAddress=emailText.Value;
如果(!IsNullOrEmpty(address1Text.Value))
user.Address1=address1Text.Value;
如果(!IsNullOrEmpty(address2Text.Value))
user.Address2=address2Text.Value;
如果(!IsNullOrEmpty(cityText.Value))
user.City=cityText.Value;
如果(!IsNullOrEmpty(provinceText.Value))
user.Province=provinceText.Value;
如果(!IsNullOrEmpty(postCodeText.Value))
user.PostalCode=postCodeText.Value;
如果(!IsNullOrEmpty(countryText.SelectedItem.Text))
user.Country=countryText.SelectedItem.Text;
user.Save();
}
公共静态bool IsNullOrEmpty(T值)
{
if(typeof(T)=typeof(string))
返回字符串.IsNullOrEmpty(值为字符串);
返回值。等于(默认值(T));
}
与其在每个字段上查找IsNullOrEmpty(数据),还有什么建议可以使用Linq或其他方法改进我的代码吗
非常感谢您的时间…您可以在属性设置器处检查是否为null或空:
public class MWCompetitionContestantsDetails
{
private string _firstName;
public string FirstName
{
get
{
return this._firstName;
}
set
{
if(!IsNullOrEmpty(value))
{
this._firstName = value;
}
}
}
.........
public static bool IsNullOrEmpty<T>(T value)
{
if (typeof(T) == typeof(string))
return string.IsNullOrEmpty(value as string);
return value.Equals(default(T));
}
}
public class mw竞争对手详细信息
{
私有字符串_firstName;
公共字符串名
{
得到
{
返回此。\u firstName;
}
设置
{
如果(!IsNullOrEmpty(值))
{
这个。_firstName=value;
}
}
}
.........
公共静态bool IsNullOrEmpty(T值)
{
if(typeof(T)=typeof(string))
返回字符串.IsNullOrEmpty(值为字符串);
返回值。等于(默认值(T));
}
}
您可以在属性设置器处检查是否为null或空:
public class MWCompetitionContestantsDetails
{
private string _firstName;
public string FirstName
{
get
{
return this._firstName;
}
set
{
if(!IsNullOrEmpty(value))
{
this._firstName = value;
}
}
}
.........
public static bool IsNullOrEmpty<T>(T value)
{
if (typeof(T) == typeof(string))
return string.IsNullOrEmpty(value as string);
return value.Equals(default(T));
}
}
public class mw竞争对手详细信息
{
私有字符串_firstName;
公共字符串名
{
得到
{
返回此。\u firstName;
}
设置
{
如果(!IsNullOrEmpty(值))
{
这个。_firstName=value;
}
}
}
.........
公共静态bool IsNullOrEmpty(T值)
{
if(typeof(T)=typeof(string))
返回字符串.IsNullOrEmpty(值为字符串);
返回值。等于(默认值(T));
}
}
只要属性是字符串,就可以使用对象初始化:
MWCompetitionContestantsDetails user = new MWCompetitionContestantsDetails(){
FirstName = firstNameText.Value,
LastName = lastNameText.Value,
UserEmailAddress = emailText.Value,
Address1= address1Text.Value,
Address2 = address2Text.Value,
City = cityText.Value,
Province= provinceText.Value,
PostalCode = postCodeText.Value,
Country = countryText.SelectedItem.Text
};
user.Save();
只要属性是字符串,就可以使用对象初始化:
MWCompetitionContestantsDetails user = new MWCompetitionContestantsDetails(){
FirstName = firstNameText.Value,
LastName = lastNameText.Value,
UserEmailAddress = emailText.Value,
Address1= address1Text.Value,
Address2 = address2Text.Value,
City = cityText.Value,
Province= provinceText.Value,
PostalCode = postCodeText.Value,
Country = countryText.SelectedItem.Text
};
user.Save();
首先,我想说,我喜欢Onant和Adil Mammadov的解决方案,因为它们非常简单。
不过,我已经编写了我喜欢在类似情况下使用的花式幽默方法代码,我想与大家分享:
class Save<T> : ISave
{
private readonly System.Action<T> _assignValue;
private readonly System.Func<T> _getValue;
public void Do()
{
T value = _getValue();
if (!IsNullOrEmpty(value))
{
_assignValue(value);
}
}
public static Save<T> Value<T>(System.Func<T> getValue, System.Action<T> assignValue)
{
return new Save<T>(getValue, assignValue);
}
private Save(System.Func<T> getValue, System.Action<T> assignValue)
{
_getValue = getValue;
_assignValue = assignValue;
}
private static bool IsNullOrEmpty<T>(T value)
{
if (typeof(T) == typeof(string))
return string.IsNullOrEmpty(value as string);
return value.Equals(default(T));
}
}
internal interface ISave
{
void Do();
}
public static void SaveUserData()
{
MWCompetitionContestantsDetails user = new MWCompetitionContestantsDetails();
MWCompetitionsEntryDetails entry = new MWCompetitionsEntryDetails();
new List<ISave>
{
Save<string>.Value( () => firstNameText.Value, x => user.FirstName = x),
Save<string>.Value( () => lastNameText.Value, x => user.LastName = x),
Save<int>.Value( () => age.Value, x => user.Age = x),// int's supported :)
// etc
}
.ForEach(x => x.Do());
user.Save();
}
class保存:我保存
{
私有只读系统。操作\u赋值;
private readonly System.Func\u getValue;
公营部门
{
T值=_getValue();
如果(!IsNullOrEmpty(值))
{
_转让价值(价值);
}
}
公共静态保存值(System.Func getValue、System.Action assignValue)
{
返回新保存(getValue、assignValue);
}
私有保存(System.Func getValue、System.Action assignValue)
{
_getValue=getValue;
_赋值=赋值;
}
私有静态bool IsNullOrEmpty(T值)
{
if(typeof(T)=typeof(string))
返回字符串.IsNullOrEmpty(值为字符串);
返回值。等于(默认值(T));
}
}
内部接口ISave
{
无效Do();
}
公共静态void SaveUserData()
{
MWCompetitionContentsDetails用户=新的MWCompetitionContentsDetails();
MWCompetitionEntryDetails条目=新的MWCompetitionEntryDetails();
新名单
{
Save.Value(()=>firstNameText.Value,x=>user.FirstName=x),
Save.Value(()=>lastNameText.Value,x=>user.LastName=x),
Save.Value(()=>age.Value,x=>user.age=x),//支持int:)
//等
}
.ForEach(x=>x.Do());
user.Save();
}
首先,我想说我喜欢Onant和Adil Mammadov的解决方案,因为它们非常简单。不过,我已经编写了我喜欢在类似情况下使用的花式幽默方法代码,我想与大家分享:
class Save<T> : ISave
{
private readonly System.Action<T> _assignValue;
private readonly System.Func<T> _getValue;
public void Do()
{
T value = _getValue();
if (!IsNullOrEmpty(value))
{
_assignValue(value);
}
}
public static Save<T> Value<T>(System.Func<T> getValue, System.Action<T> assignValue)
{
return new Save<T>(getValue, assignValue);
}
private Save(System.Func<T> getValue, System.Action<T> assignValue)
{
_getValue = getValue;
_assignValue = assignValue;
}
private static bool IsNullOrEmpty<T>(T value)
{
if (typeof(T) == typeof(string))
return string.IsNullOrEmpty(value as string);
return value.Equals(default(T));
}
}
internal interface ISave
{
void Do();
}
public static void SaveUserData()
{
MWCompetitionContestantsDetails user = new MWCompetitionContestantsDetails();
MWCompetitionsEntryDetails entry = new MWCompetitionsEntryDetails();
new List<ISave>
{
Save<string>.Value( () => firstNameText.Value, x => user.FirstName = x),
Save<string>.Value( () => lastNameText.Value, x => user.LastName = x),
Save<int>.Value( () => age.Value, x => user.Age = x),// int's supported :)
// etc
}
.ForEach(x => x.Do());
user.Save();
}
class保存:我保存
{
私有只读系统。操作\u赋值;
private readonly System.Func\u getValue;
公营部门
{
T值=_getValue();
如果(!IsNullOrEmpty(值))
{
_转让价值(价值);
}
}
公共静态保存值(System.Func getValue、System.Action assignValue)
{
返回新保存(getValue、assignValue);
}
私有保存(System.Func getValue、System.Action assignValue)
{