Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

c# - 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??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...