Home > OS >  date edit using guid key
date edit using guid key

Time:04-15

I have used guid as primary key for my table.

namespace entities
{
    public class position 
    {
        [Key]
        public Guid positionId { get; set; }
        public string Name { get; set; }
    }
}

From my view page, I am sending this positionId as string and then to the method GetById

How to use the Guid for data edit? I am trying to do:

public JsonResult GetById(string id)
        {
            try
            {
                var data = (from z in db.positions
                            where z.positionId == id
                            select z).ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

This query is showing error as: Operator == can not be applied to operands of type string and guid. How to convert this string id to type Guid?

CodePudding user response:

Why don't just use Guid instead of string? Anyway to convert a string to Guid you should use the Guid.Parse() method

CodePudding user response:

By following Kvble answer:

public JsonResult GetById(string id)
        {
            try
            {
                var data = (from z in db.positions
                            where z.positionId == Guid.Parse(id);
                            select z).ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
  • Related