ServiceStack OrmLite包含Where子句但不选择的列

时间:2017-01-10 20:54:41

标签: c# .net sql-server servicestack ormlite-servicestack

考虑我在下面的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子句中,但我不希望在结果中也选择该列组。有没有办法将它包含在哪里但是从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'

1 个答案:

答案 0 :(得分:2)

如果您不想填充整个POCO,则需要指定仅包含您想要选择的字段的自定义选择,例如:

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
   }));