ram
ram

Reputation: 29

Concatenating Two Different IEnumerable Objects in C#

I have got two different IEnumerable objects, however properties are almost similar in both the classes,now I want to concatenate/merge them, so both the results can be assigned to repeater datasource, below is the sample code.

IEnumerable<ICachedItem> cacheOld = Cache<String>.CachedItems; //My Old Cache Class, here I am fetching all the cached Items
IEnumerable<Caching.ICachedItem> cacheNew = Caching.Cache<String>.CachedItems; //My New class to get the cached items   
var combined = cacheNew.Cast<ICachedItem>().Concat(cacheOld); //Trying to concat both
repeater.DataSource = combined.OrderBy(entry => entry.Region + ":" + entry.Prefix + ":" + entry.Key); //Assigning to Datasource
repeater.DataBind();

combined object is coming blank, any suggestions.

Update: Currently we have got these in the class

public class CachedItem<T>: ICachedItem
{

    public CachedItem(String key, String prefix, T value)
        : this(CacheRegion.Site, prefix, key, value)
    {
    }    
}

Do we need to modify this class?

Upvotes: 0

Views: 314

Answers (1)

Hadi Brais
Hadi Brais

Reputation: 23639

You can use Select to convert new items to old items. Assuming you have the appropriate constructor, you can do this:

var combined = cacheNew.Select(newItem => new OldCachedItem(newItem)).Concat(cacheOld);

The result would be of type IEnumerable<ICachedItem>. You should replace OldCachedItem with name of the old cached item type that implements ICachedItem.

Upvotes: 2

Related Questions