假设我有一个实体对象'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查询并取回对象包括Birthstone,因此我可以将该对象绑定到一个表示Birthstone的网格,我不会将它存储在任何地方,因为它始终是计算的(这是假装数据,如果不合逻辑)。
List<Jewel> jewels = new List<Jewel>;
using (jewelentities db = new jewelentities())
{
jewels = (from j in db.Jewels select j).ToList();
}
如何用名称,出生日期和幸运石填充我的宝石对象?
如果我在这里没有遵循最佳实践,请告诉我!
编辑
我已经尝试添加一个部分类的实体部分类。当我现在参考Jewel类时,它会看到Birthstone属性,但它是空的。我不知道为什么?下面是部分类:
public partial class Jewel
{
private string _birthstone;
public string Birthstone
{
get { return _birthstone; }
set
{
JewelBusiness jewelBusiness = new JewelBusiness();
_birthstone = jewelBusiness.RequestBirthstone(birthmonth);
}
}
}
如果我使用LINQ查询实体获得的宝石记录列表,我得到的所有来自实体的信息,Jewel.Birthstone是存在的,但它是空的。然而,如果我对结果进行foreach ---
foreach (Jewel j in jewels)
{
string stone = jewelBusiness.RequestBirthstone(j.Birthmonth);
}
石头将等于预期的结果(该月的诞生石)。
为什么我的部分课程没有回归诞生?
看看我的更新答案 – 2013-04-29 08:46:08
我想改变的属性get方法,并检查该字段为空,是要走的路。但是请记住,您不能在entityframe-work查询中将扩展属性用于实体,因此您必须先使用toList,然后查询扩展属性。 – 2013-04-29 08:57:46