Often applications allow a user to specify inputs by providing the IDs, or unique names of items which can be found in the database.
Using the user provided collection of items, you would query your data and hopefully return a collection with the same number of elements as what the user was searching for.
In some cases though, not all the items the user provided will be found in the database. I recently wrote a generic helper method which provides warnings to the user for any "missing" elements (i.e. they asked for it in the source but it wasn't in the output).
public static class MissingItemsHelper
{
public static string GetWarningsIfItemsNotInCollection<T>(
IEnumerable<T> itemsToFind,
IEnumerable<T> collectionToSearch,
Func<T, string> missingItemMessageTextSelector,
string messageNounText)
{
IEnumerable<T> missingItems =
(from sourceItem in itemsToFind
where collectionToSearch.Any((f) => f.Equals(sourceItem)) == false
select sourceItem);
if (missingItems.Count() > 0)
return string.Format("The following {0}s were not found:\r\n", messageNounText)
+ string.Join("\r\n",missingItems.Select(missingItemMessageTextSelector).ToArray());
return string.Empty;
}
}
I've tried to keep it as generic as possible, which is why you must provide the type T of the elements and a function which selects the "missing" information as well as a noun which describes that information.
For example:
MissingItemsHelper.GetWarningsIfItemsNotInCollection<long>(
inputIds, (from d in dataItems select d.UniqueId),
new Func<long, string>((id) => id.ToString()), "Unique ID");
This could be extended further to take various types with a comparison operator, but for simplicity I have kept to one type.