PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 5, 2022

[FIXED] how to remove a character at the end of a string in linq c#

 September 05, 2022     linq, trim     No comments   

Issue

I have the following LINQ query; I want to remove dot character if available at the end of string

var hatoList = (from L in db.vHatoList select L.HatoFullName.Trim('.')).ToList();

Solution

You're currently trying to perform the query in the database. We don't know what database technology you're using, so it's hard to know whether there's some way to make it actually happen in the database, but it's probably simplest to do in memory instead (given that it won't change how many records will be returned). The AsEnumerable() method is used to just change the compile-time type to IEnumerable<T> (instead of IQueryable<T>) forcing the rest of the query to be evaluated in .NET code instead of in the database:

var hatoList = db.vHatoList 
                 .AsEnumerable()
                 .Select(entry => entry.HatoFullName.TrimEnd('.'))
                 .ToList();

(I've also changed the call from Trim to TrimEnd to avoid it removing periods at the start of the string.)



Answered By - Jon Skeet
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing