什么时候内插字符串评估?

时间:2016-11-04 13:02:16

标签: c# clr

我有以下代码:

    internal class Constants
    {
        internal static string Source { get; set; }

        #region EvaluationRepository
        internal static string QUERY_001 = $@"
select
  e.*
from {Source} e
where
  e.id = @Id
";

        internal static string QUERY_002 = $@"
select
  e.*
from {Source} e
where
  e.statusid=@StatusId
  and e.begindate >= @FromDate
  and e.enddate <= @ToDate
";

        internal static string QUERY_003
        {
            get
            {
                return $@"
select
  d.statusid [StatusId],
  count(1) [Count]
from
  (select e.statusid
  from {Source} e
  where e.begindate >= @FromDate and e.enddate <= @ToDate) d
group by d.statusid
";
            }
        }
        #endregion
    }

唯一一次填充{Source}是我将查询公开为属性(QUERY_003)
当它作为字段公开时,它不起作用。 (QUERY_001,QUERY_002)

任何人都可以解释原因吗?由于静态(不确定这是否是一个单词)?

很抱歉SQL的逐字插值噪音:)

1 个答案:

答案 0 :(得分:3)

它在运行时完成。它与使用string.Format等效(低于生成的IL)。

字段未正确填充的原因是因为Source在最初执行时为空(初始化静态类时)。

相关问题