我有一个类Projects,其定义如下:
public enum ContractFreq
{
[Display(Name="Monthly")]
M = 1,
[Display(Name="Quarterly")]
Q = 2,
[Display(Name="Anually")]
A = 3,
[Display(Name="One-Time")]
X = 4
}
public partial class Project
{
[Key]
[Display(Name="Project")]
public int ProjectID { get; set; }
[Display(Name = "Contract Frequency")]
public ContractFreq ContractFreq { get; set; }
[Required]
public string Description { get; set; }
[ForeignKey("Vendor")]
public int VendorID { get; set; }
public int Cost { get; set; }
[Display(Name = "Project Start Date")]
public DateTime? DateStarted { get; set; }
[Display(Name = "Project Name")]
private String _ProjectName = Vendor.VendorName + " - [" + Description + "]";
[Display(Name = "Project Name")]
public String ProjectName
{
get
{
return _ProjectName;
}
set
{
this._ProjectName = Vendor.VendorName + " - [" + this.Description + "]";
}
}
/*
public string ProjectName
{
get
{
return Vendor.VendorName + " - [" + Description + "]";
}
}
*/
/* Navigation */
public virtual Vendor Vendor { get; set; }
public virtual ICollection<PurchReq> PurchReqs { get; set; }
public virtual ICollection<Invoice> Invoices { get; set; }
public Project()
{
DateStarted = System.DateTime.Now;
PurchReqs = new HashSet<PurchReq>();
}
我想将字段“ProjectName”作为Vendor.VendorName和Project.Description的连接。我知道字符串参数不能为空。什么是实现这个的最简单,最简单的方法?我原来用过:
public string ProjectName
{
get
{
return Vendor.VendorName + " - [" + Description + "]";
}
}
但是我无法将linq与非静态字段一起使用,并且想知道是否有更好的方法。
答案 0 :(得分:0)
它可以是这样的:
[Display(Name = "Project Name")]
public String ProjectName
{
get
{
return string.Format("{0} - [{1}]", Vendor.VendorName, Description );
}
}
并且没有静态成员read this。