{"record":{"id":"d31a88656f02b3ca","repo":"bchavez/Bogus","slug":"the-list-is-empty-there-are-no-items-to-select","errorCode":null,"errorMessage":"The list is empty. There are no items to select.","messagePattern":"The list is empty\\. There are no items to select\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Randomizer.cs","lineNumber":534,"sourceCode":"\n      return Shuffle(array).Take(count.Value).ToArray();\n   }\n\n   /// <summary>\n   /// Get a random list item.\n   /// </summary>\n   public T ListItem<T>(List<T> list)\n   {\n      return ListItem(list as IList<T>);\n   }\n\n   /// <summary>\n   /// Get a random list item.\n   /// </summary>\n   public T ListItem<T>(IList<T> list)\n   {\n      if (list.Count <= 0)\n         throw new ArgumentException(\"The list is empty. There are no items to select.\", nameof(list));\n\n      var r = Number(max: list.Count - 1);\n      return list[r];\n   }\n\n   /// <summary>\n   /// Get a random subset of a List.\n   /// </summary>\n   /// <param name=\"items\">The source of items to pick from.</param>\n   /// <param name=\"count\">The number of items to pick; otherwise, a random amount is picked.</param>\n   public List<T> ListItems<T>(IList<T> items, int? count = null)\n   {\n      if( count > items.Count )\n         throw new ArgumentOutOfRangeException(nameof(count));\n      if( count is null )\n         count = Number(0, items.Count - 1);\n\n      return Shuffle(items).Take(count.Value).ToList();","sourceCodeStart":516,"sourceCodeEnd":552,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Randomizer.cs#L516-L552","documentation":"Thrown by Randomizer.ListItem<T>(IList<T>) when list.Count <= 0. Requires at least one element to pick from. The param is named 'list'.","triggerScenarios":"Calling f.Random.ListItem(new List<T>()) or passing an empty IList from a query result.","commonSituations":"Empty results from a filtered LINQ query passed directly to ListItem; collection not yet populated at call time.","solutions":["Verify list.Count > 0 before calling ListItem.","Ensure the source collection is populated upstream.","Provide a fallback when the list is empty."],"exampleFix":"// before\nvar item = f.Random.ListItem(filtered.ToList());\n// after\nvar list = filtered.ToList();\nvar item = list.Count > 0 ? f.Random.ListItem(list) : defaultItem;","handlingStrategy":"validation","validationCode":"static T SafeListItem<T>(Bogus.Randomizer r, IList<T> list, T fallback) {\n    return list.Count > 0 ? r.ListItem(list) : fallback;\n}","typeGuard":"bool HasItems<T>(IList<T> list) => list.Count > 0;","tryCatchPattern":"try { return r.ListItem(list); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"list is empty\")) {\n    return default;\n}","preventionTips":["Verify list.Count > 0 before calling ListItem.","Populate lists before sampling.","Provide fallbacks for query-derived lists."],"tags":["bogus","randomizer","listitem","empty-collection"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}