ServiceStack OrmLite 在 Where 子句中包含列但不包含 Select
ServiceStack OrmLite Include Column in Where Clause but Not Select
考虑下面的 LoyaltyCard
数据库 DTO:
[Alias("customer_ids")]
[CompositeIndex("id_code", "id_number", Unique = true)]
public class LoyaltyCard
{
[Alias("customer_code")]
public int CustomerId { get; set; }
[Alias("id_code")]
public string LoyaltyCardCode { get; set; }
[Alias("id_number")]
public string LoyaltyCardNumber { get; set; }
[Alias("date_issued ")]
public DateTime? IssueDate { get; set; }
[Alias("linked_id")]
public bool LinkedId { get; set; }
[Alias("positive_id")]
public bool PositiveId { get; set; }
}
我将 CustomerId
作为 属性,因为我需要将其包含在生成的 SQL 的 WHERE
子句中,但我不希望列也被 selected 在结果集中。有没有办法将它包含在 where 中但从 select?
中排除
// I was hoping that result would not have a populated CustomerIds
var result = db.Select<LoyaltyCard>(id => id.LoyaltyCardCode == "PS" && id.CustomerId == customerCode);
我已尝试添加 [Ignore]
属性,但因 SqlException: Invalid column name 'CustomerId'
而失败。
如果您不想填充整个 POCO,您需要指定一个自定义 Select,其中仅包含您要选择的字段,例如:
var result = db.Select<LoyaltyCard>(db.From<LoyaltyCard>()
.Where(id => id.LoyaltyCardCode == "PS" && id.CustomerId == customerCode)
.Select(x => new {
x.LoyaltyCardCode,
x.LoyaltyCardNumber,
x.IssueDate,
x.LinkedId,
x.PositiveId
}));
考虑下面的 LoyaltyCard
数据库 DTO:
[Alias("customer_ids")]
[CompositeIndex("id_code", "id_number", Unique = true)]
public class LoyaltyCard
{
[Alias("customer_code")]
public int CustomerId { get; set; }
[Alias("id_code")]
public string LoyaltyCardCode { get; set; }
[Alias("id_number")]
public string LoyaltyCardNumber { get; set; }
[Alias("date_issued ")]
public DateTime? IssueDate { get; set; }
[Alias("linked_id")]
public bool LinkedId { get; set; }
[Alias("positive_id")]
public bool PositiveId { get; set; }
}
我将 CustomerId
作为 属性,因为我需要将其包含在生成的 SQL 的 WHERE
子句中,但我不希望列也被 selected 在结果集中。有没有办法将它包含在 where 中但从 select?
// I was hoping that result would not have a populated CustomerIds
var result = db.Select<LoyaltyCard>(id => id.LoyaltyCardCode == "PS" && id.CustomerId == customerCode);
我已尝试添加 [Ignore]
属性,但因 SqlException: Invalid column name 'CustomerId'
而失败。
如果您不想填充整个 POCO,您需要指定一个自定义 Select,其中仅包含您要选择的字段,例如:
var result = db.Select<LoyaltyCard>(db.From<LoyaltyCard>()
.Where(id => id.LoyaltyCardCode == "PS" && id.CustomerId == customerCode)
.Select(x => new {
x.LoyaltyCardCode,
x.LoyaltyCardNumber,
x.IssueDate,
x.LinkedId,
x.PositiveId
}));