Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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# 扩展实体对象以包括计算特性_C#_Linq_Entity Framework_Asp.net 4.5 - Fatal编程技术网

C# 扩展实体对象以包括计算特性

C# 扩展实体对象以包括计算特性,c#,linq,entity-framework,asp.net-4.5,C#,Linq,Entity Framework,Asp.net 4.5,假设我有一个实体对象“Jewels”,它的属性为“Name”和“Birthdate”。 我想实现一个LINQ查询,它返回一个具有“Name”、“Birthdate”和“Birthstone”的对象。因此,我将“珠宝”扩展如下: public partial class JewelStones : Jewels string Birthstone = null; public void JewelsWithStone() { this.Birthstone = "diamond";

假设我有一个实体对象“Jewels”,它的属性为“Name”和“Birthdate”。 我想实现一个LINQ查询,它返回一个具有“Name”、“Birthdate”和“Birthstone”的对象。因此,我将“珠宝”扩展如下:

public partial class JewelStones : Jewels

string Birthstone = null;
public void JewelsWithStone()
{
     this.Birthstone = "diamond";
      //(we figure out what stone applies to the month here)
}
我可以做到这一点,我认为我的思路是正确的,但我不知道如何编写LINQ查询并返回包含诞生石的对象,因此我可以将该对象绑定到一个网格,该网格将显示诞生石,我不会将其存储在任何地方,因为它总是经过计算的(这是假装数据,如果不符合逻辑,请抱歉)

如果我使用LINQ查询该实体以获取珠宝记录列表,我将从该实体获取所有信息,即Jewel.Birthstone在那里,但它为空。但是如果我对结果做一个预测---

石头将等于预期结果(该月的诞生石)


为什么我的分部类不返回诞生石???

你的Jewels EntityObject不是也在分部类中吗?您很可能只是添加一个Jewels分部类来“扩展”它,并在那里添加想要的属性。

我不确定是否正确理解您的要求。但是,如果您不想存储诞生石,而是想动态计算它,只需将代码更改为

public partial class Jewel
{
    private string _birthstone;
    public string Birthstone
    {
        get 
        { 
             if (_birthstone == null)
             {
                  JewelBusiness jewelBusiness = new JewelBusiness();
                  _birthstone = jewelBusiness.RequestBirthstone(birthmonth); 
             }
             return _birthstone; 
        }
    }
}

对我来说,这取决于计算列的逻辑所在的位置

若它驻留在数据库中,那个么您必须在Linq中执行连接查询。我假设在本例中,您有一个名为BirthStoneTable的表,以月份作为关系。我不建议在linq查询中添加三元操作,比如
select j.BirthDate.Month==1?“钻石”://等
。它很难调试和跟踪(而且由于代码覆盖率的原因)

如果它位于特定于UI的位置(只是为了改进显示),我通常会添加一个类型转换类,例如:

public class JewelUI{
  public explicit operator JewelUI(Jewel jewel){
    JewelUI jewelUI = new JewelUI();
    // assign birthdate and name
    jewelUI.BirthStone = GetBirthStone(jewel.BirthDate.Month);
  }

  public string BirthStone{get;set;};

  public string GetBirthStone(int month){
    if(month == 1) return "Diamond";
    //etc etc
  }
}
如果在业务逻辑中使用计算列,通常我在服务/业务逻辑中处理计算。所有这些都是为了确保良好的关注分离


注意:我可能误解了您的要求,尽管我正试图这么做,但我不确定如何实现它,或者这是否是在绑定之前向实体对象添加属性的最佳方式。我认为您确实理解了,并且您的示例很有意义。简而言之,我需要一个对象,该对象具有数据库中的属性,然后是一些。请查看我更新的回答我认为更改属性的get方法并检查字段是否为null是一种方法。但请记住,不能在entityframe查询中对实体使用扩展特性,因此必须先使用toList,然后查询扩展特性。
foreach (Jewel j in jewels)
{
    string stone = jewelBusiness.RequestBirthstone(j.Birthmonth);
}
public partial class Jewel
{
    private string _birthstone;
    public string Birthstone
    {
        get 
        { 
             if (_birthstone == null)
             {
                  JewelBusiness jewelBusiness = new JewelBusiness();
                  _birthstone = jewelBusiness.RequestBirthstone(birthmonth); 
             }
             return _birthstone; 
        }
    }
}
public class JewelUI{
  public explicit operator JewelUI(Jewel jewel){
    JewelUI jewelUI = new JewelUI();
    // assign birthdate and name
    jewelUI.BirthStone = GetBirthStone(jewel.BirthDate.Month);
  }

  public string BirthStone{get;set;};

  public string GetBirthStone(int month){
    if(month == 1) return "Diamond";
    //etc etc
  }
}