动态地将数据集映射到 StringTemplate
我在 Windows 服务通知项目 (.NET 4.0) 中使用 StringTemplate 。我的目标是读入并处理定义查询和字符串模板的 XML 文件。然后,该类将 DataTable 中的结果绑定到用于通过电子邮件发送的模板中。下面是一个 XML 片段以及 LINQ 语句和用于执行绑定的 C#。
<!-- XML Data Original-->
<Items>
<Item>
<Query>
Select name, title, date from People;
</Query>
<Template>
Welcome $name to $title$ on $date$.
</Template>
</Item>
</Items>
var dataTable = database.GetQuery(query);
var data = (from dataRow in dataTable.AsEnumerable()
select new
{
Name = dataRow.Field<string>("Name"),
Title = dataRow.Field<string>("Title"),
Date = dataRow.Field<DateTime>("Date")
}).Distinct();
stringTemplate.SetAttribute("Name", data.Name);
stringTemplate.SetAttribute("Title", data.Title);
stringTemplate.SetAttribute("Date", data.Date);
问题是上面的C#是静态的,我想使它动态。如果我需要在 XML 查询元素中添加另一个字段和相应的模板字段,该怎么办?
<!-- XML Data Modified-->
<Items>
<Item>
<Query>
Select name, title, date, location from People;
</Query>
<Template>
Welcome $name to $title$ on $date$ at $location$.
</Template>
</Item>
</Items>
DataTable 包含新列,但我的 LINQ 语句和绑定代码不包含。我的问题是我可以采用什么策略来动态获取数据表中的数据并将其绑定到我的字符串模板?请注意,我当前正在使用 LINQ,但解决方案不必这样做。
I am using StringTemplate in a Windows Service notification project (.NET 4.0). My objective is to read in and process XML files that define a query and a stringtemplate. The class then binds the results from a DataTable into the template for emailing. Below is an XML snippet and the LINQ statement and the C# to do the binding.
<!-- XML Data Original-->
<Items>
<Item>
<Query>
Select name, title, date from People;
</Query>
<Template>
Welcome $name to $title$ on $date$.
</Template>
</Item>
</Items>
var dataTable = database.GetQuery(query);
var data = (from dataRow in dataTable.AsEnumerable()
select new
{
Name = dataRow.Field<string>("Name"),
Title = dataRow.Field<string>("Title"),
Date = dataRow.Field<DateTime>("Date")
}).Distinct();
stringTemplate.SetAttribute("Name", data.Name);
stringTemplate.SetAttribute("Title", data.Title);
stringTemplate.SetAttribute("Date", data.Date);
The problem is the above C# is static, I would like to make it dynamic. That is what if I need to add another field in the XML query element and a corresponding template field?
<!-- XML Data Modified-->
<Items>
<Item>
<Query>
Select name, title, date, location from People;
</Query>
<Template>
Welcome $name to $title$ on $date$ at $location$.
</Template>
</Item>
</Items>
The DataTable contains the new column, however my LINQ statement and binding code does not. My question is what strategy could I employ to fetch and bind the data from the DataTable to my stringtemplate dynamically? Note I am currently using LINQ but the solution does not have to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我采取的方法不是投影到匿名类型中,而是在字符串模板文件中迭代集合。我也以相同的方式处理单个记录“静态”,但我在 Linq 语句上使用 FirstOrDefault 过滤器。
C#:
模板:
The approach I took is not projecting into an anonymous type and in the String Template file iterating over the collection. I also treat a single record "static" in the same manner but I use a FirstOrDefault filter on the Linq statement.
C#:
Template: