assistance to refactor similar methods

M

mike

I've got two methods that do basically the same thing, just with
different parameters. See the method below.

public IEnumerable<FaqListItem> GetItemsOfCategory(string category)
{
IEnumerable<FaqListItem> itemsOfCategory =
from item in items.GetItems()
where item.Category == category
select new FaqListItem
{
Title = item.Title,
Question=item.Question,
Answer=item.Answer,
Category=item.Category
};

return itemsOfCategory;
}

I have also written a GetItemsOfTitle method that uses the item's
title instead. I'm looking for .NET 3.0 / 3.5 ways in which I can
refactor these methods into one into which I can pass in the field
name as well as the method. I think I've seen something like this
using Func<> or something but I'm not sure anymore.

I'm also interested in finding out if I can simplify my select as item
is of the same type as what I'm generating.

Thanks
Mike
 
A

Alberto Poblacion

mike said:
I've got two methods that do basically the same thing, just with
different parameters. See the method below.

public IEnumerable<FaqListItem> GetItemsOfCategory(string category)
{
IEnumerable<FaqListItem> itemsOfCategory =
from item in items.GetItems()
where item.Category == category
select new FaqListItem
{
Title = item.Title,
Question=item.Question,
Answer=item.Answer,
Category=item.Category
};

return itemsOfCategory;
}

I have also written a GetItemsOfTitle method that uses the item's
title instead. I'm looking for .NET 3.0 / 3.5 ways in which I can
refactor these methods into one into which I can pass in the field
name as well as the method. I think I've seen something like this
using Func<> or something but I'm not sure anymore.

I believe that the following can achieve what you want (typed out from
memory, untested):

public IEnumerable<FaqListItem> GetItemsByPredicate(Predicate<ItemType> p)
{
IEnumerable<FaqListItem> itemsOfCategory =
items.GetItems().Where(p)
.Select(item => new FaqListItem
{
Title = item.Title,
Question=item.Question,
Answer=item.Answer,
Category=item.Category
});
return itemsOfCategory;
}

Invoke it as
result=GetItemsByPredicate(item=>item.Category == category)
or
result=GetItemsByPredicate(item=>item.Title == title)

I'm also interested in finding out if I can simplify my select as item
is of the same type as what I'm generating.

If I understand what you mean, the type returned by items.GetItems
("ItemType" in the preceding example) is precisely "FaqListItem". In this
case, the method can be simplified a lot:

public IEnumerable<FaqListItem> GetItemsByPredicate(Predicate<FaqListItem>
p)
{
return items.GetItems().Where(p);
}
 
M

mike

Hi Alberto

Thanks for the help. It appears I've got some thing sto learn about
lambdas as delegates and such.

Cheers
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top