Click here to Skip to main content
15,891,431 members
Home / Discussions / C#
   

C#

 
QuestionDisposing issue when trying to load data from SQLite database Pin
Code4Ever14-Jul-22 20:22
Code4Ever14-Jul-22 20:22 
AnswerRe: Disposing issue when trying to load data from SQLite database Pin
Richard MacCutchan14-Jul-22 21:18
mveRichard MacCutchan14-Jul-22 21:18 
GeneralRe: Disposing issue when trying to load data from SQLite database Pin
Code4Ever14-Jul-22 21:30
Code4Ever14-Jul-22 21:30 
QuestionCould not Load type spride Pin
Luis M. Rojas7-Jul-22 4:58
Luis M. Rojas7-Jul-22 4:58 
AnswerRe: Could not Load type spride Pin
Pete O'Hanlon7-Jul-22 21:39
mvePete O'Hanlon7-Jul-22 21:39 
QuestionRe: Could not Load type spride Pin
Richard MacCutchan7-Jul-22 23:29
mveRichard MacCutchan7-Jul-22 23:29 
QuestionJSDOC '@extends' is not attached to class Pin
Luis M. Rojas4-Jul-22 5:00
Luis M. Rojas4-Jul-22 5:00 
AnswerRe: JSDOC '@extends' is not attached to class Pin
Pete O'Hanlon4-Jul-22 5:04
mvePete O'Hanlon4-Jul-22 5:04 
GeneralRe: JSDOC '@extends' is not attached to class Pin
Luis M. Rojas4-Jul-22 5:18
Luis M. Rojas4-Jul-22 5:18 
GeneralRe: JSDOC '@extends' is not attached to class Pin
Pete O'Hanlon4-Jul-22 19:55
mvePete O'Hanlon4-Jul-22 19:55 
GeneralRe: JSDOC '@extends' is not attached to class Pin
Luis M. Rojas5-Jul-22 2:33
Luis M. Rojas5-Jul-22 2:33 
AnswerRe: JSDOC '@extends' is not attached to class Pin
Richard MacCutchan4-Jul-22 5:49
mveRichard MacCutchan4-Jul-22 5:49 
QuestionVisual Studio Web Performance and Load Testing Tools alternative Pin
j11codep3-Jul-22 23:11
j11codep3-Jul-22 23:11 
AnswerRe: Visual Studio Web Performance and Load Testing Tools alternative Pin
Pete O'Hanlon3-Jul-22 23:54
mvePete O'Hanlon3-Jul-22 23:54 
GeneralRe: Visual Studio Web Performance and Load Testing Tools alternative Pin
j11codep4-Jul-22 0:18
j11codep4-Jul-22 0:18 
QuestionLinq to SQL With Enum On Null Field [MODIFIED] Pin
Kevin Marois1-Jul-22 17:49
professionalKevin Marois1-Jul-22 17:49 
I have a table that store person data. There are NamePrefix and NameSuffix columns in the data. Both are nullable.

Here is the Persons table
CREATE TABLE [dbo].[Persons]
(
    Id                          INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
    RemoteId                    UNIQUEIDENTIFIER NOT NULL,
    ClientRepId                 INT NULL FOREIGN KEY (ClientRepId) REFERENCES Persons(Id),
    SupervisorId                INT NULL FOREIGN KEY (SupervisorId) REFERENCES Persons(Id),
    PersonType                  INT NOT NULL,
    Prefix                      INT NULL,
    FirstName                   VARCHAR(MAX),
    LastName                    VARCHAR(MAX),
    NickName                    VARCHAR(MAX),
    Suffix                      INT NULL,
    Title                       VARCHAR(MAX)
)
Here is the PersonEntity
namespace CLOI.Entities
{
    public class PersonEntity : _EntityBase
    {
        private PersonType _PersonType;
        public PersonType PersonType
        {
            get { return _PersonType; }
            set
            {
                SetProperty<PersonType>("PersonType", ref _PersonType, value);
            }
        }

        private NamePrefix _Prefix;
        public NamePrefix Prefix
        {
            get { return _Prefix; }
            set
            {
                SetProperty<NamePrefix>("Prefix", ref _Prefix, value);
            }
        }

        private string _FirstName;
        public string FirstName
        {
            get { return _FirstName; }
            set
            {
                SetProperty<string>("FirstName", ref _FirstName, value);
            }
        }

        private string _LastName;
        public string LastName
        {
            get { return _LastName; }
            set
            {
                SetProperty<string>("LastName", ref _LastName, value);
            }
        }

        private string _NickName;
        public string NickName
        {
            get { return _NickName; }
            set
            {
                SetProperty<string>("NickName", ref _NickName, value);
            }
        }

        private NameSuffix _Suffix;
        public NameSuffix Suffix
        {
            get { return _Suffix; }
            set
            {
                SetProperty<NameSuffix>("Suffix", ref _Suffix, value);
            }
        }

        private string _Title;
        public string Title
        {
            get { return _Title; }
            set
            {
                SetProperty<string>("Title", ref _Title, value);
            }
        }

        public string FullName
        {
            get { return $"{FirstName} {LastName}"; }
        }

        private int _ClientRepId;
        public int ClientRepId
        {
            get { return _ClientRepId; }
            set
            {
                SetProperty<int>("ClientRepId", ref _ClientRepId, value);
            }
        }
    }
}

I'm trying to get this to work:
results = (from p in dc.Persons
            select new PersonEntity
            {
                RemoteId = p.RemoteId,
                ClientRepId = p.ClientRepId ?? 0,
                SupervisorId = p.SupervisorId ?? 0,
                PersonType = (PersonType)p.PersonType ?? PersonType.None,
                Prefix = (NamePrefix)p.Prefix ?? NamePrefix.None,
                FirstName = p.FirstName,
                LastName = p.LastName,
                NickName = p.NickName,
                Suffix = (NameSuffix)p.Suffix ?? NameSuffix.None,
                Title = p.Title,
                CreatedById = p.CreatedById,
                CreatedDT = p.CreatedDT,
                LastModifiedById = p.Id,
                LastModifiedDT = p.ModifiedByDT.Value,
                DeletedById = p.DeletedById,
                DeletedDT = p.DeletedByDT.Value
            }).Where(predicate).ToList();
It's throwing a null ref exception because both the NamePrefix and NameSuffix columns are null.

I tried to change those lines like this:
Prefix = (NamePrefix)p.Prefix ?? NamePrefix.None,

But that won't compile with "
Operator '??' cannot be applied to operands of type 'PersonType' and 'PersonType'
"

What's the right way to do this?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.


modified 2-Jul-22 15:40pm.

AnswerRe: Linq to SQL With Enum On Null Field Pin
OriginalGriff1-Jul-22 18:43
mveOriginalGriff1-Jul-22 18:43 
GeneralRe: Linq to SQL With Enum On Null Field Pin
Kevin Marois2-Jul-22 9:40
professionalKevin Marois2-Jul-22 9:40 
GeneralRe: Linq to SQL With Enum On Null Field Pin
Dave Kreskowiak2-Jul-22 9:49
mveDave Kreskowiak2-Jul-22 9:49 
GeneralRe: Linq to SQL With Enum On Null Field Pin
Kevin Marois2-Jul-22 9:59
professionalKevin Marois2-Jul-22 9:59 
GeneralRe: Linq to SQL With Enum On Null Field Pin
OriginalGriff2-Jul-22 9:52
mveOriginalGriff2-Jul-22 9:52 
GeneralRe: Linq to SQL With Enum On Null Field Pin
Kevin Marois2-Jul-22 9:59
professionalKevin Marois2-Jul-22 9:59 
QuestionRe: Linq to SQL With Enum On Null Field Pin
Eddy Vluggen2-Jul-22 10:37
professionalEddy Vluggen2-Jul-22 10:37 
AnswerRe: Linq to SQL With Enum On Null Field Pin
Kevin Marois2-Jul-22 14:58
professionalKevin Marois2-Jul-22 14:58 
AnswerRe: Linq to SQL With Enum On Null Field Pin
Dave Kreskowiak2-Jul-22 4:59
mveDave Kreskowiak2-Jul-22 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.