Jorge
Jorge

Reputation: 18237

split field a string in a linq

I tried in different ways to accomplish this but I can make it work I have a class called CampoConfiguracionVista Defined like this

public class CampoConfiguracionVista
{
    public CampoConfiguracionVista() 
    { 

    }

    public int IDCampo { get; set; }
    public int IDConfiguracion { get; set; }
    public string Nombre { get; set; }
    public bool ValidationEspecial { get; set; }
    public bool Requerido { get; set; }
    public int IDTipodato { get; set; }
    public int? minimo { get; set; }
    public int? maximo { get; set; }
    public string[] valores { get; set; }
}

And I have linq where I have i field called Valores which contains an string value separate by ; So What to I want to Accomplish it's split this field value into a string array I tried in this two ways :

first: all in one linq

    var query = (from T in db.ConfiguracionCampo
             where T.IDTipificacion == idTipificacion
             && T.Campo.Activo == true
             select new CampoConfiguracionVista()
             {
                 IDCampo = T.Campo.IDCampo,
                 IDTipodato = T.IDTipodato,
                 ValidationEspecial = T.ValidationEspecial,
                 minimo = T.minimo,
                 maximo = T.minimo,
                 Requerido = T.Requerido,
                 Nombre = T.Campo.Nombre,
                 valores = T.Valores.Split(';')
             }).ToList();

Second: I think that the problem was the linq can't translate the split to sql so i made two linqs like this *

var query = (from T in db.ConfiguracionCampo
                         where T.IDTipificacion == idTipificacion
                         && T.Campo.Activo == true
                         select T);

var camposConfigurados = (from D in query select D).Select(C => new CampoConfiguracionVista()
            {
                IDCampo = C.Campo.IDCampo,
                IDTipodato = C.IDTipodato,
                ValidationEspecial = C.ValidationEspecial,
                minimo = C.minimo,
                maximo = C.minimo,
                Requerido = C.Requerido,
                Nombre = C.Campo.Nombre,
                valores = C.Valores.Split(';')
            }).ToList();

What am I doing wrong??

Upvotes: 0

Views: 3368

Answers (3)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112324

You are splitting by ',' instead of ';'.


The last query can be simplified like this

var camposConfigurados = query
    .AsEnumerable() // <== this makes succeeding queries run with LINQ-to-Objects.
    .Select(C => new CampoConfiguracionVista() { 
        IDCampo = C.Campo.IDCampo, 
        IDTipodato = C.IDTipodato, 
        ValidationEspecial = C.ValidationEspecial, 
        minimo = C.minimo, 
        maximo = C.minimo, 
        Requerido = C.Requerido, 
        Nombre = C.Campo.Nombre, 
        valores = C.Valores.Split(';') 
    }).ToList(); 

Upvotes: 1

RichardOD
RichardOD

Reputation: 29157

You are right, some things you can't do with LINQ to SQL (an example is here). To get round the problem you simply need to do the bits that aren't in LINQ to SQL, with LINQ to Objects, so you need to convert IQueryable to IEnumerable, using something like AsEnumerable

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

If you force the data into memory before running the select by calling AsEnumerable(), I think your query should run fine.

var camposConfigurados = (from D in query select D)
    .AsEnumerable()
    .Select(C => new CampoConfiguracionVista()
        {
            IDCampo = C.Campo.IDCampo,
            IDTipodato = C.IDTipodato,
            ValidationEspecial = C.ValidationEspecial,
            minimo = C.minimo,
            maximo = C.minimo,
            Requerido = C.Requerido,
            Nombre = C.Campo.Nombre,
            valores = C.Valores.Split(',')
        }).ToList();

Upvotes: 2

Related Questions