问题
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??
回答1:
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
回答2:
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();
回答3:
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();
来源:https://stackoverflow.com/questions/9283752/split-field-a-string-in-a-linq