Aliendreamer

export xml ef core exam

Aug 13th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1.  public static string ExportPrisonersInbox(SoftJailDbContext context, string prisonersNames)
  2.         {
  3.             var names = prisonersNames.Split(',', StringSplitOptions.RemoveEmptyEntries).ToArray();
  4.  
  5.             var prisoners = context.Prisoners
  6.                 .Where(x=>names.Any(i=>i==x.FullName))
  7.                 .ToArray()
  8.                 .Select(x=>new ExportPrisonerDto
  9.                 {
  10.                     Id = x.Id,
  11.                     Name = x.FullName,
  12.                     IncarcerationDate = x.IncarcerationDate.ToString("yyyy-MM-dd",CultureInfo.InvariantCulture),
  13.                     EncruptedMessages = x.Mails.Select(m=>new ExportMessageDto
  14.                     {      
  15.                        
  16.                      Description =new string(m.Description.Reverse().ToArray())
  17.  
  18.                     }).ToArray()
  19.                 })
  20.                 .OrderBy(x=>x.Name)
  21.                 .ThenBy(x=>x.Id)
  22.                 .ToArray();
  23.  
  24.             StringBuilder sb = new StringBuilder();
  25.             var xmlNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
  26.             var serializer = new XmlSerializer(typeof(ExportPrisonerDto[]), new XmlRootAttribute("Prisoners"));
  27.             serializer.Serialize(new StringWriter(sb),prisoners, xmlNamespaces);
  28.  
  29.             return sb.ToString();
  30.         }
  31.  
  32. // ето как излиза :Д
  33. <Prisoners>
  34.   <Prisoner>
  35.     <Id>3</Id>
  36.     <Name>Binni Cornhill</Name>
  37.     <IncarcerationDate>1967-04-29</IncarcerationDate>
  38.     <EncruptedMessages>
  39.       <Message>
  40.         <Description>!?sdnasuoht evif-ytnewt rof deksa uoy ro orez artxe na ereht sI</Description>
  41.       </Message>
  42.     </EncruptedMessages>
  43.   </Prisoner>
  44. ......
Advertisement
Add Comment
Please, Sign In to add comment