0

我有以下条目的数据表:

**Companyid   Deptid    Location    Employeeid Employeename    Employeeage**       
001           D001      CA           0001      Jason Bourne     57
001           D001      CA           0002      Will Smith       45
001           D001      NV           0003      Kurt Rusell      47
002           D002      CA           0008      Panda            57
002           D002      CA           0009      Fox              45
002           D002      NV           0010      Wolf             35

我希望使用 Linq 在 C# 中创建 XML。公司 ID、部门 ID 和位置的组合将被视为唯一。我希望创建一个新的公司元素以及该公司元素下的所有员工。XML 应该看起来像

<companies>
<company companyid="001" DeptID="D001" Location="CA">
  <Employee id="0001" Employeename="Jason Bourne" Employeeage=57/>
  <Employee id="0002" Employeename="Will Smith"   Employeeage=45/>
</company>
<company companyid="001" DeptID="D001" Location="NV">
  <Employee id="0003" Employeename="Kurt Rusell" Employeeage=47/>
 </company>
<company companyid="002" DeptID="D002" Location="CA">
  <Employee id="0008" Employeename="Panda" Employeeage=57/>
  <Employee id="0009" Employeename="Fox"   Employeeage=45/>
</company>
<company companyid="002" DeptID="D002" Location="NV">
<Employee id="0010" Employeename="Wolf"  Employeeage=35/>
</company>
</companies>

任何帮助将不胜感激。数据表也按 companyid、deptid、location 排序

4

2 回答 2

0
var xml = new XElement("companies",
             from e in db.Employees // use your DbSet name here
             group e by new { e.Companyid, e.Deptid, e.Location } into g
             select new XElement("company",
                 new XAttribute("companyid", g.Key.Companyid),
                 new XAttribute("DeptID", g.Key.Deptid),
                 new XAttribute("Location", g.Key.Location),
                 from x in g
                 select new XElement("Employee",
                           new XAttribute("id", x.Employeeid),
                           new XAttribute("Employeename", x.Employeename),
                           new XAttribute("Employeeage", x.Employeeage))
             ));

主要思想很简单 - 当您使用 Linq 构建 xml 时,您可以提供任何对象作为元素的内容。因此,我创建companies根元素并提供将返回该元素内容的查询。查询按三个字段对实体进行分组,并company为每个组创建元素(用于company元素属性的组键)。最后一部分 - 从每个组中我选择Employee元素并将它们添加到company节点的内容中。

于 2013-08-15T23:41:01.453 回答
0
var query =
    from row in table.AsEnumerable()
    group row by new
    {
        CompanyId = row.Field<string>("CompanyId"),
        DeptId = row.Field<string>("DeptId"),
        Location = row.Field<string>("Location")
    }
    into g
    select new XElement("company",
            new XAttribute("CompanyId", g.Key.CompanyId),
            new XAttribute("DeptId", g.Key.DeptId),
            new XAttribute("Location", g.Key.Location),
            from row in g
            select new XElement(
                "employee",
                new XAttribute("id", row.Field<string>("id")),
                new XAttribute("EmployeeName", row.Field<string>("EmployeeName")),
                new XAttribute("EmployeeAge", row.Field<string>("EmployeeAge"))));

var document = new XDocument(new XElement("companies", query));
于 2013-08-15T23:38:27.333 回答