Cannot implicity Convert type 'System.Collections.Generic.Ienumerable' to 'System.Collections.Generic.List'

Asked

Viewed 2,589 times

1

I have the following method below for the purpose of returning notifications that must be displayed to a particular customer and notifications that must be displayed to all customers.

public List<TNotification> GetNotifications(TClient client)
{
    var notificacoesClient = Session.CreateCriteria<TNotification>()
        .CreateAlias("TClientNotifications", "clientNotifications", JoinType.LeftOuterJoin)
        .SetFetchMode("clientNotifications.Client", FetchMode.Eager)
        .CreateAlias("clientNotifications.Client", "client", JoinType.LeftOuterJoin)
        .Add(Restrictions.Eq("client.Id", client.Id))
        .AddOrder(Order.Desc("Id"))
        .List<TNotification>()
        .ToList();

    var notificacoesAll = Session.CreateCriteria<TNotification>()
        .Add(Restrictions.Eq("IsToAll", true))
        .AddOrder(Order.Desc("Id"))
        .List<TNotification>()
        .ToList();

    return notificacoesAll.Union(notificacoesClient);
}

But in education return notificacoesAll.Union(notificacoesClient); is returning the following message:

Cannot implicity Convert type 'System.Collections.Generic.Ienumerable' to 'System.Collections.Generic.List'. An Explicit Conversion exists (are you Missing a cast?)

How could I resolve this issue?

2 answers

6


This mistake happens because notificacoesAll.Union(notificacoesClient) returns a IEnumerable<TNotification>, but the signature of your method waits List<TNotification>.

So you have two alternatives:

  • Change the signature of your method to return IEnumerable<TNotification>
  • Turn your union result into a list:

    return notificacoesAll.Union(notificacoesClient).ToList();
    

One more detail: I don’t think we need to call ToListin the queries of their example, because List already returns a list (is redundant).

2

Take off the .ToList() of the list and leave only on return

Something like that:

public List<TNotification> GetNotifications(TClient client)
{
    var notificacoesClient = Session.CreateCriteria<TNotification>()
        .CreateAlias("TClientNotifications", "clientNotifications", JoinType.LeftOuterJoin)
        .SetFetchMode("clientNotifications.Client", FetchMode.Eager)
        .CreateAlias("clientNotifications.Client", "client", JoinType.LeftOuterJoin)
        .Add(Restrictions.Eq("client.Id", client.Id))
        .AddOrder(Order.Desc("Id"))
        .List<TNotification>();

    var notificacoesAll = Session.CreateCriteria<TNotification>()
        .Add(Restrictions.Eq("IsToAll", true))
        .AddOrder(Order.Desc("Id"))
        .List<TNotification>();

    return notificacoesAll.Union(notificacoesClient).ToList();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.