Alhambra Eidos
Alhambra Eidos

Reputation: 1553

convert code to use Linq

I have the following code, I want to use Linq, any possible ??

I have one list of users, and I need generate other list.

any help ? thanks in advance, greetings

 SortedList<ClaveUsuarioPersonal, UsuarioRe> users = new SortedList<ClaveUsuarioPersonal, UsuarioRe>();

            if (this.UsuarioRe.UsuarioSar != null)
            {
                foreach (DC.AccesosUsuario var in this.UsuarioRe.UsuarioSar.ListaAccesos)
                {
                    if (var != null && var.Aprobado.Value)
                    {
                        UsuarioRe usuario = UserContextBLL.ObtenerUsuarioRe(var.Login);
                        if(usuario!=null && usuario.UsuarioAire!=null)
                        {
                            if (!usuario.UsuarioAire.Empresa.HasValue)
                            {
                                Log.Trace(Utility.LogCategory, "No existe empresa para el usuario de Aire " + usuario.UsuarioAire.Login);
                            }
                            else
                            {
                                ClaveUsuarioPersonal clave = new ClaveUsuarioPersonal(Convert.ToInt32(usuario.UsuarioAire.Empresa.Value), usuario.UsuarioAire.Login);

                                users.Add(clave, usuario);
                            }
                        }
                    }
                }
            }
            return users;

Upvotes: 0

Views: 80

Answers (2)

RoelF
RoelF

Reputation: 7573

try installing Resharper 5.0 Nightly Build. It has a lot of features to convert for/foreach to linq statements. It might be helpful for those who want to learn LINQ. More info on these features here.

Edit: Jetbrains released Resharper 5.0 Beta a few days ago. Seems to be more stable than the nightly builds.

Upvotes: 1

Matt Ellen
Matt Ellen

Reputation: 11592

var users = from u in this.UsuarioRe.UsuarioSar.ListaAccesos
            where (UserContextBLL.ObtenerUsuarioRe(u.Login) != null) 
                && (UserContextBLL.ObtenerUsuarioRe(u.Login).UsuarioAire != null)
                && (UserContextBLL.ObtenerUsuarioRe(u.Login).UsuarioAire.Empressa.HasValue)
            select (new ClaveUsuarioPersonal(Convert.ToInt32(UserContextBLL.ObtenerUsuarioRe(u.Login).UsuarioAire.UsuarioAire.Empresa.Value), UserContextBLL.ObtenerUsuarioRe(u.Login).UsuarioAire.Login));

I can't test this because I don't have your classes. Also I'm not sure how to implement the logging feature in the the LINQ expression. It should get you the list though.

Upvotes: 0

Related Questions