C# 如何在C语言中删除linq数据中的空格
我有linq,其中一列中的数据可能在字符串之间有空格或特殊字符,例如我的调查,你的调查。我需要删除它,这样在过滤后linq应该返回mysurvey和yourssurvey 列I有兴趣删除空格,特殊字符是consultation=consultation.Name 我正在使用C.net核心和实体框架C# 如何在C语言中删除linq数据中的空格,c#,entity-framework,linq,.net-core,C#,Entity Framework,Linq,.net Core,我有linq,其中一列中的数据可能在字符串之间有空格或特殊字符,例如我的调查,你的调查。我需要删除它,这样在过滤后linq应该返回mysurvey和yourssurvey 列I有兴趣删除空格,特殊字符是consultation=consultation.Name 我正在使用C.net核心和实体框架 var query = (from consultation in Context.Consultations join survey in Co
var query = (from consultation in Context.Consultations
join survey in Context.Surveys on consultation.Id equals survey.ConsultationId into surveys
select new
{
consultationId = consultation.Id,
consultation = consultation.Name,
surveyId = surveys.FirstOrDefault() == null? null : surveys.Select(x=>x.Id),
survey = surveys.FirstOrDefault() == null ? null : surveys.Select(x => x.Name),
subject ="survey"
});
如果要删除字符串中的空格和特殊字符,请使用以下命令:
consultation = Regex.Replace(consultation.Name, "[^0-9A-Za-z]+", "");
使用名称空间
using System.Text.RegularExpressions;
如果要删除字符串中的空格和特殊字符,请使用以下命令:
consultation = Regex.Replace(consultation.Name, "[^0-9A-Za-z]+", "");
使用名称空间
using System.Text.RegularExpressions;
编写一个像这样的扩展方法
public static string StripSpacesAndSpecialCharacters(this string text)
{
var specChars = new string[] { "'", ";" }; // add more spec chars here
var procesesedString = text.Trim();
foreach (var spec in specChars)
{
procesesedString = procesesedString.Replace(spec, string.Empty);
}
return procesesedString;
}
然后在查询中使用它,如
var query = (from consultation in Context.Consultations
join survey in Context.Surveys on consultation.Id equals survey.ConsultationId into surveys
select new
{
consultationId = consultation.Id,
consultation = consultation.Name.StripSpacesAndSpecialCharacters(),
surveyId = surveys.FirstOrDefault() == null? null : surveys.Select(x=>x.Id),
survey = surveys.FirstOrDefault() == null ? null : surveys.Select(x => x.Name),
subject ="survey"
});
编写一个像这样的扩展方法
public static string StripSpacesAndSpecialCharacters(this string text)
{
var specChars = new string[] { "'", ";" }; // add more spec chars here
var procesesedString = text.Trim();
foreach (var spec in specChars)
{
procesesedString = procesesedString.Replace(spec, string.Empty);
}
return procesesedString;
}
然后在查询中使用它,如
var query = (from consultation in Context.Consultations
join survey in Context.Surveys on consultation.Id equals survey.ConsultationId into surveys
select new
{
consultationId = consultation.Id,
consultation = consultation.Name.StripSpacesAndSpecialCharacters(),
surveyId = surveys.FirstOrDefault() == null? null : surveys.Select(x=>x.Id),
survey = surveys.FirstOrDefault() == null ? null : surveys.Select(x => x.Name),
subject ="survey"
});
LINQ只是一种查询语言。这不是ORM。您使用的是哪种ORM?为什么不使用关系?ORM的工作是从关系和导航属性生成连接。您只需要在上下文中从cons中写入任何内容。cons中的调查中的咨询。调查…或者您可以在上下文中从调查中写入。调查选择新建{survey.Consultation.ID,survey.ID..}等。我使用的是实体FrameworkInq,它只是一种查询语言。这不是ORM。您使用的是哪种ORM?为什么不使用关系?ORM的工作是从关系和导航属性生成连接。您只需要在上下文中从cons中写入任何内容。cons中的调查中的咨询。调查…或者您可以在上下文中从调查中写入。调查选择新的{survey.Consultation.ID,survey.ID..}等。我正在使用实体框架。这不起作用。正则表达式不能作为可查询的,只能作为可枚举的,这是预期的,因为reqex代码不能转换为SQL。如果要使用正则表达式,首先必须将所选属性(包括不需要的字符)移动到本地进程AsEnumerable,然后才能使用包含REQUEXT的新Select。这将不起作用。正则表达式不能作为可查询的,只能作为可枚举的,这是预期的,因为reqex代码不能转换为SQL。如果要使用正则表达式,首先必须将所选属性(包括不需要的字符)移动到本地进程AsEnumerable,然后才能使用包含REQUEX的新Select