在linq中将字段拆分为字符串

在linq中将字段拆分为字符串

本文介绍了在linq中将字段拆分为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了不同的方法来实现此目的,但是我可以使它正常工作,我有一个名为CampoConfiguracionVista的类,定义如下

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; }
}

我在linq中有一个名为Valores的字段,其中包含一个用;分隔的字符串值.那么我要完成的工作是将该字段值拆分为我用以下两种方法尝试过的字符串数组:

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 :

第一名:一字不漏

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

我在做什么错??

推荐答案

您是对的,有些事情是您无法使用LINQ to SQL进行的().为了解决该问题,您只需要使用LINQ to Objects来处理LINQ to SQL中没有的位,因此您需要使用可枚举

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

这篇关于在linq中将字段拆分为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:04