将字段分割为 linq 中的字符串
我尝试以不同的方式来完成此任务,但我可以让它工作我有一个名为 CampoConfiguracionVista
的类,定义如下
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; }
}
我有 linq,其中有一个名为 Valores
的字段,其中包含一个由 ;
分隔的字符串值 那么我想要完成什么,它将这个字段值分割成一个字符串数组,我以这两种方式尝试过:
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();
第二:我认为问题是 linq 无法将分割转换为 sql,所以我做了两个像这样的 linq *
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();
我做错了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在通过调用 AsEnumerable() 运行选择之前将数据强制写入内存,我认为您的查询应该运行良好。
If you force the data into memory before running the select by calling
AsEnumerable()
, I think your query should run fine.你是对的,有些事情你不能用 LINQ to SQL 做(示例在这里)。要解决这个问题,您只需使用 LINQ to Objects 执行 LINQ to SQL 中没有的部分,因此您需要使用类似 AsEnumerable
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
您将按
','
而不是';'
进行拆分。最后一个查询可以像这样简化
You are splitting by
','
instead of';'
.The last query can be simplified like this