更新:
我已经将我的 xml 变成了coldfusion中的查询表,所以这可能有助于解决这个问题。
所以我的数据是:
[id] | [code] | [desc] | [supplier] | [name] | [price]
------------------------------------------------------
1 | ABCDEF | "Tst0" | "XYZ" | "Test" | 123.00
2 | ABCDXY | "Tst1" | "XYZ" | "Test" | 130.00
3 | DCBAZY | "Tst2" | "XYZ" | "Tst2" | 150.00
现在我需要的是下面的 linq to xml 查询输出。输出应该是这样的(我会用 JSON 编写,这样我更容易输入):
[{
"code": "ABCD",
"name": "Test",
"products":
{
"id": 1,
"code": "ABCDEF",
"desc": "Tst0",
"price": 123.00
},
{
"id": 2,
"code": "ABCDXY",
"desc": "Tst1",
"price": 130.00
}
},
{
"code": "DCBA",
"name": "Tst2",
"products":
{
"id": 3,
"code": "DCBAZY",
"desc": "Tst2",
"price": 150.00
}
}]
如您所见,按“CODE”和“Supplier”代码的前 4 个字符分组。
谢谢
如何将以下 LINQ to XML 查询转换为 SQL?
from q in query
group q by new { Code = q.code.Substring(0, 4), Supplier = q.supplier } into g
select new
{
code = g.Key.Code,
fullcode = g.FirstOrDefault().code,
supplier = g.Key.Supplier,
name = g.FirstOrDefault().name,
products = g.Select(x => new Product { id = x.id, c = x.code, desc = string.IsNullOrEmpty(x.desc) ? "Description" : x.desc, price = x.price })
}
我能想到的最好的:
SELECT c, supplier, n
FROM products
GROUP BY C, supplier, n
不知道如何在那里获取子查询或获取代码的子字符串。
ps:这是为了coldfusion,所以我猜他们的sql版本可能与ms sql不同..