Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How to convert Lazy<List<T>> to Lazy<List<U>>?

I have a Lazy<List<T>> where T is a class which has a huge string and metadata about files. Let's call them Property HugeString and Property Metadata

I have this class U, which has the same property HugeString, among other things. I have to convert Lazy<List<T>> to Lazy<List<U>> without loading all stuff.

Is it possible ?

This is where I create my list, and inside that method I get info about the file and the file itself:

entity.VersionedItems =
    new Lazy<List<VersionedItemEntity>>(
        () => VersionedItemEntity.GetFromTFSChanges(entity,chng.Changes));

This is what I want to do (commented)

ChangesetList.Add(
    new HistoryLogEntryModel()
    {
        Revision = changeset.Changeset.ToString(),
        Author = changeset.User,
        Date = changeset.Date.ToString("dd/MM/yyyy"),
        Message = changeset.Comment,
        //VersionedItems = changeset.VersionedItems
    }

But HistoryLogEntryModel has a different Version of VersionedItems. And I need to convert some variables.If I would convert one thing to another, it would load up everything and that would be unnecessary and slow.

Is this the right approach? How else could I achieve this?

thanks in adv.

~

like image 767
Conrad Clark Avatar asked Oct 11 '22 15:10

Conrad Clark


1 Answers

You should be able to wrap the Lazy<List<T>> in a Lazy<List<U>>.

var uLazy = new Lazy<List<U>>(() => tLazy.Value.Select(t => (U)t).ToList());
like image 171
ChaosPandion Avatar answered Oct 19 '22 01:10

ChaosPandion