0

I'm creating a ASP.NET project and I have a CustomerModel generated by Entity Framework (because database first). I'm trying to manipulate a Name field so I can separate it into a FirstName and LastName respectively. Here's my model:

public partial class Customer
{
    public int CustomerID { get; set; }
    public string Name { get; set; }
    ...

    // something like this?
    // I know this will cause an error though
    FirstName = Name.Substring(0, Name.IndexOf(" "));

Am I correct when I manipulate it like this? Also would this affect anything when I, for instance, create data and it the value goes to variable instead of field.

  • you will probably have an easier time with `string.Split` – TheGeneral Jul 29 '18 at 06:07
  • Possible duplicate of [How to separate full name string into firstname and lastname string?](https://stackoverflow.com/questions/6629136/how-to-separate-full-name-string-into-firstname-and-lastname-string) – TheGeneral Jul 29 '18 at 06:09
  • I'm asking about splitting the data I get from the model not necessarily how to properly split a string. I just put that as an example –  Jul 29 '18 at 06:11
  • 2
    The best way you need to make separate fields for firstName and lastName. – Asif Raza Jul 29 '18 at 06:12
  • I probably should have said this in my answer, but you may just want to leave it as is. It's culturally insensitive to try to shoehorn people into using a western standard for their name. And once you went the one way, it's like trying to turn hamburger back into a cow. – Jim Berg Jul 29 '18 at 08:28
  • Too bad for Billy Jean King. – Gert Arnold Jul 29 '18 at 10:24

2 Answers2

0

You can go with string.Split().

For example:

string name = "Gabe Newel";
string FirstName = name.Split(" ")[0];
string LastName = name.Split(" ").Last();
s0me1
  • 109
  • 7
0

How do you know that a person doesn't enter three or four names? A lot of people have middle names. How would you handle that logically? What about people with names where their last name is two parts like Van Pelt or Saint James?

If you can somehow define that, you can use logic in your setter for the Name property. When it's set, split it to its various parts. Here's a simple example:

    public class NameExample
    {
        private string name;
        private string firstName;
        private string lastName;

        public string Name
        {
            get { return name;  }
            set
            {
                if( value != name )
                {
                    name = value;
                    if (string.IsNullOrEmpty(name)) return;
                    var names = name.Split(' ');
                    firstName = names[0];
                    if (names.Length > 1) lastName = names[1]; // Is our person Cher? ;-)
                }
            }
        }

        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
                name = firstName + (string.IsNullOrEmpty(lastName) ? "" : " " + lastName);
            }
        }

        public string LastName
        {
            get => lastName;
            set {
                lastName = value;
                name = firstName + (string.IsNullOrEmpty(lastName) ? "" : " " + lastName);
            }
        }

You can do this with Entity Framework since it actually calls the setters for the properties when it loads the values from the database. And it calls the getters to get the values to write to the database. This is why you must use public properties instead of public fields for domain models.

My example is deliberately verbose and quick. You can fine tune it hoever you want.

Jim Berg
  • 609
  • 4
  • 7
  • To me this only confirms that it's a hopeless effort. What about people having two (or more) first names? Or Chinese names? OP's only chance is to receive name parts separately from a human being (directly or indirectly). – Gert Arnold Jul 29 '18 at 17:59