[英]Linq to SQL column computed from query to different table?
Is it possible to define a column in one of my Linq to SQL entities such that it's calculated as part of the SQL query? 是否可以在我的Linq to SQL实体之一中定义一列,使其作为SQL查询的一部分进行计算?
Here is my Entity class so far: 到目前为止,这是我的Entity类:
[Table(Name = "Products")]
public class ProductEntity
{
[Column(Name = "ModelNumber", IsPrimaryKey = true)]
public string ModelNumber { get; set; }
[Column(Name = "SerialNumber", IsPrimaryKey = true)]
public string SerialNumber { get; set; }
}
I want to add a new property like: 我想添加一个新的属性,如:
[Column]
public bool ActiveListing { get; set; }
Except its value should be calculated based on a complicated Linq query which I am executing elsewhere right now (not in SQL!): 除了它的值应基于我现在正在其他地方执行的复杂Linq查询(不是在SQL中!):
context.GetTable<SalesRecords>().
Where(ah => ah.Date <= GlobalCoordinatedDateTime.Local).
Where(ah => ah.ProductModelNumber == ModelNumber && ah.ProductSerialNumber == SerialNumber).
OrderByDescending(ah => ah.Date).
FirstOrDefault().Status == Statuses.Active;
My best guess is that I could use AssociationAttribute
but I don't know how to do that when there could be more than one match in the "SalesRecords" table and I want to get the first after applying some ordering. 我最好的猜测是我可以使用
AssociationAttribute
但是当“ SalesRecords”表中可能有多个匹配项并且我想在应用一些排序后获得第一个匹配项时,我不知道该怎么做。
EDIT: My question is not the same as a simple "computed column" as seen in this question , I need to compute the value using data stored in different tables, outside of the entity its self. 编辑:我的问题与在此问题中看到的简单“计算列”不同,我需要使用存储在实体自身之外的不同表中的数据来计算值。
if you don't need to store the column at database you can use: 如果您不需要将列存储在数据库中,则可以使用:
[Table(Name = "Products")]
public class ProductEntity
{
// your other columns...
[NotMapped]
public bool ActiveListing {
get
{
bool result = false;
// your logic to calculate then set to "result" variable
return result;
}
}
}
but if you need to store it, change the name of ActiveListing property, then manually assign to the final ActiveListing property before you will create or update the record. 但是如果需要存储它,请更改ActiveListing属性的名称,然后在创建或更新记录之前手动将其分配给最终的ActiveListing属性。 Example:
例:
[Table(Name = "Products")]
public class ProductEntity
{
// your other columns...
[NotMapped]
public bool CalculateActiveListing
{
get
{
bool result = false;
// your logic to calculate then set to "result" variable
return result;
}
}
public bool ActiveListing { get; set; }
}
here an example if you have a navigation property to SalesRecords. 这是一个示例,如果您具有SalesRecords的导航属性。 important have Lazy Loading enabled, or use the Include() method.
重要的是启用了延迟加载,或使用Include()方法。
[NotMapped]
public bool CalculateActiveListing
{
get
{
bool result = false;
// your logic to calculate then set to "result" variable.
// for example:
// validate SalesRecords has data
if (this.SalesRecords != null)
{
var sale = this.SalesRecords
.Where(ah => ah.Date <= GlobalCoordinatedDateTime.Local)
.Where(ah => ah.ProductModelNumber == ModelNumber && ah.ProductSerialNumber == SerialNumber)
.OrderByDescending(ah => ah.Date)
.FirstOrDefault();
// sale exists
if (sale != null)
{
result = sale.Status == Statuses.Active;
}
}
return result;
}
}
another example using your DbContext: 使用DbContext的另一个示例:
[NotMapped]
public bool CalculateActiveListing
{
get
{
bool result = false;
// your logic to calculate then set to "result" variable.
// for example:
using (var context = new MyDbContext())
{
var sale = context.SalesRecords
.Where(ah => ah.Date <= GlobalCoordinatedDateTime.Local)
.Where(ah => ah.ProductModelNumber == ModelNumber && ah.ProductSerialNumber == SerialNumber)
.OrderByDescending(ah => ah.Date)
.FirstOrDefault();
// sale exists
if (sale != null)
{
result = sale.Status == Statuses.Active;
}
}
return result;
}
}
sorry, my bad english. 对不起,我的英语不好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.