如果表的列是XML,那么是否可以将Linq- to -SQL和Linq-to-XML结合起来?
发布于 2010-01-14 02:10:54
如果“组合”的意思是“让Linq to SQL将查询转换成实际的SQL Server XML查询”,那么我相信答案是否定的。但是没有什么可以阻止您提取XML数据并在其上运行本地XML查询。
要回答“在此场景中如何使用XML列”这个更一般的问题,最好的方法是创建一个用户定义的函数来处理XML,如果可以的话。然后,您可以使用Linq to SQL来执行UDF。
如果做不到这一点,我将采用存储过程路线。
发布于 2010-01-14 01:46:59
是的,这当然是可能的。您可以在单个查询中组合任意数量的IEnumerable源。例如,这里有一篇文章展示了如何将L2S与Linq to Excel结合使用:
http://blogs.msdn.com/ericwhite/archive/2008/12/03/joining-linq-to-sql-and-linq-to-excel.aspx
兰迪
发布于 2010-01-14 05:09:28
是的,这是很有可能的,这是Joeseph Rattz的Pro Linq的一个例子:
using (NorthwindDataContext DB = new NorthwindDataContext())
{
string StatesXml =
@"<States>
<State ID=""OR"" Description=""Oregon"" />
<State ID=""WA"" Description=""Washington"" />
<State ID=""CA"" Description=""California"" />
<State ID=""ID"" Description=""Idaho"" />
<State ID=""MT"" Description=""Montana"" />
<State ID=""NM"" Description=""New Mexico"" />
</States>";
XElement States = XElement.Parse(StatesXml);
var Custs = (from c in DB.Customers
where c.Country == "USA"
select c).AsEnumerable().Join(
States.Elements("State"),
c => c.Region,
s => (string)s.Attribute("ID"),
(c, s) => new
{
Customer = c,
State = (string)s.Attribute("Description")
});
foreach (var Cust in Custs)
{
Console.WriteLine("Customer = {0} : {1} : {2}",
Cust.Customer.CompanyName,
Cust.Customer.Region,
Cust.State);
}下面是将输出的内容:
五大湖食品市场:俄勒冈州
饥饿郊狼进口商店:或:俄勒冈州
Lazy K Kountry商店:华盛顿
让我们停止N商店: CA : California
寂寞松树餐厅:或:俄勒冈
响尾蛇峡谷杂货店:新墨西哥州
Save-a-lot Markets : ID : Idaho
大块头奶酪:或:俄勒冈
饼干盒子:蒙大拿州
Trail的负责人美食供应者:华盛顿
白三叶市场:华盛顿州
https://stackoverflow.com/questions/2058934
复制相似问题