{"_id":"56bafe8ccec63e0d00f0d2eb","project":"553dec691a946a0d00ad6f27","__v":3,"category":{"_id":"5543585f795b590d001dc89a","version":"553dec691a946a0d00ad6f2a","__v":9,"pages":["554358c8b7f4540d00fcef43","55435b69b7f4540d00fcef46","55435bf2795b590d001dc8a3","55435ec262b30e0d004b1706","555636f626e9bc0d0044ea81","55563946ea5e120d00188550","555639cc26e9bc0d0044ea8d","5561dd7bb40338210035f905","56bafe8ccec63e0d00f0d2eb"],"project":"553dec691a946a0d00ad6f27","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-05-01T10:41:35.279Z","from_sync":false,"order":1,"slug":"usage","title":"Usage"},"version":{"_id":"553dec691a946a0d00ad6f2a","project":"553dec691a946a0d00ad6f27","__v":2,"createdAt":"2015-04-27T07:59:37.477Z","releaseDate":"2015-04-27T07:59:37.477Z","categories":["553dec691a946a0d00ad6f2b","5543585f795b590d001dc89a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"user":"54c4b05742190d0d00f5fbde","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-02-10T09:10:36.284Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":999,"body":"When using builders to create lists of items it is often required to add values to a property that are selected from a secondary list of possible values.\n\nDossier provides this functionality through the Pick class, which has static methods that return different Dossier data sources based on the strategy that you want to implement. Data sources have a Data property, which is the list of data for the specified type, and a Next method, which returns the next item from the list according to the strategy the data source implements.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"RandomItemFrom\"\n}\n[/block]\nAllows you to select random items from a secondary list. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var addresses = Builder<Address>.CreateListOfSize(15).BuildList();\\nvar customers = Builder<Customer>\\n    .CreateListOfSize(15)\\n    .All()\\n    .Set(x => x.PostalAddress, Pick.RandomItemFrom(addresses).Next)\\n    .BuildList();\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNote, that Next is passed as a func - as opposed to with the brackets, such as Next() - so that it will be called each time an item in the list is constructed and produce a different value for each item. If you added the brackets then Next would only be executed once and that same value would be applied to every item in the list.\n\nFor example, a list of days of the week might return these values:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Thursday\\nSaturday\\nSaturday\\nWednesday\\nThursday\\nFriday\\nMonday\\nSunday\\nThursday\\nMonday\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nNote that the list is random and some items in the list might not get used.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"RepeatingSequenceFrom\"\n}\n[/block]\nAllows you to select items from a list in order. Once the list is complete it will go back to the start and continue to select items from the list in order.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var addresses = Builder<Address>.CreateListOfSize(3).BuildList();\\nvar customers = Builder<Customer>\\n    .CreateListOfSize(9)\\n    .All()\\n    .Set(x => x.PostalAddress, Pick.RepeatingSequenceFrom(addresses).Next)\\n    .BuildList();\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nFor example, a list of seasons would return these values:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Spring\\nSummer\\nAutumn\\nWinter\\nSpring\\nSummer\\nAutumn\\nWinter\\nSpring\\nSummer\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]","excerpt":"The Pick class allows you to select items from a list according to different strategies.","slug":"picking-list-items","type":"basic","title":"Picking list items"}

Picking list items

The Pick class allows you to select items from a list according to different strategies.

When using builders to create lists of items it is often required to add values to a property that are selected from a secondary list of possible values. Dossier provides this functionality through the Pick class, which has static methods that return different Dossier data sources based on the strategy that you want to implement. Data sources have a Data property, which is the list of data for the specified type, and a Next method, which returns the next item from the list according to the strategy the data source implements. [block:api-header] { "type": "basic", "title": "RandomItemFrom" } [/block] Allows you to select random items from a secondary list. [block:code] { "codes": [ { "code": "var addresses = Builder<Address>.CreateListOfSize(15).BuildList();\nvar customers = Builder<Customer>\n .CreateListOfSize(15)\n .All()\n .Set(x => x.PostalAddress, Pick.RandomItemFrom(addresses).Next)\n .BuildList();", "language": "csharp" } ] } [/block] Note, that Next is passed as a func - as opposed to with the brackets, such as Next() - so that it will be called each time an item in the list is constructed and produce a different value for each item. If you added the brackets then Next would only be executed once and that same value would be applied to every item in the list. For example, a list of days of the week might return these values: [block:code] { "codes": [ { "code": "Thursday\nSaturday\nSaturday\nWednesday\nThursday\nFriday\nMonday\nSunday\nThursday\nMonday", "language": "text" } ] } [/block] Note that the list is random and some items in the list might not get used. [block:api-header] { "type": "basic", "title": "RepeatingSequenceFrom" } [/block] Allows you to select items from a list in order. Once the list is complete it will go back to the start and continue to select items from the list in order. [block:code] { "codes": [ { "code": "var addresses = Builder<Address>.CreateListOfSize(3).BuildList();\nvar customers = Builder<Customer>\n .CreateListOfSize(9)\n .All()\n .Set(x => x.PostalAddress, Pick.RepeatingSequenceFrom(addresses).Next)\n .BuildList();", "language": "csharp" } ] } [/block] For example, a list of seasons would return these values: [block:code] { "codes": [ { "code": "Spring\nSummer\nAutumn\nWinter\nSpring\nSummer\nAutumn\nWinter\nSpring\nSummer", "language": "text" } ] } [/block]