1

Possuo o seguinte método abaixo com o propósito de retornar notificações que devem ser exibidas para determinado cliente e notificações que devem ser exibidas para todos os clientes.

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);
}

Porém na instrução return notificacoesAll.Union(notificacoesClient); está retornando a seguinte mensagem:

Cannot implicity convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

Como poderia resolver esta questão?

2 Respostas 2

6

Este erro acontece porque notificacoesAll.Union(notificacoesClient) retorna um IEnumerable<TNotification>, mas a assinatura do seu método espera List<TNotification>.

Logo, você tem duas alternativas:

  • Alterar a assinatura do seu método para retornar IEnumerable<TNotification>
  • Transformar o resultado da sua união em uma lista:

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

Mais um detalhe: creio que não seja necessário chamar ToListnas queries do seu exemplo, pois List já retorna uma lista (é redundante).

2

Tire o .ToList() das lista e deixe apenas no retorno

Algo assim:

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();
}
1

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .