# Changing these two functions into one function

I have two functions that are similar. The only differences is that they're using two different models. How can I change them into one function?

 ITagRepository tagRepo = new TagRepository();
ICategoryRepository catRepo = new CategoryRepository();

{
var allTags = tagRepo.GetAllQueryAble();
string[] tag = tags.Split(',');

foreach (var item in tag)
{
if (allTags.Where(e => e.Name.Contains(item)).Count() == 0)
{
{
Name = item.ToString(),
LastModifiedDate = DateTime.Now,
IsDeleted = false
});
}
}
}

{
var allCategory = catRepo.GetAllQueryAble();
string[] Category = Categories.Split(',');

foreach (var item in Category)
{
if (allCategory.Where(e => e.Name.Contains(item)).Count() == 0)
{
{
Name = item.ToString(),
LastModifiedDate = DateTime.Now,
IsDeleted = false
});
}
}
}

• Why do you use ToArray in one, but not the other? Jul 24 '14 at 8:15
• Thank you for pointing that out, did not notice it Jul 24 '14 at 8:17

You need to think about the underlying model. Have a think about isA/hasA. Depending on what you conclude...

• Are the types defining similar interfaces? You may find that the date info is like a label that both object types have attached to them. (It looks that way.) Define an interface that contains the common names. Have each of the two types implement it. Now you can use just one loop.
• Is one of them actually a member of the other? Then define it that way. Your loop will get smaller.

Bottom line is that highly repetitive code should make you think about whether your model is correct.

• This is true. I am going to look into the model design. Jul 24 '14 at 8:34