EF Core - How to Exclude a Certain Value Before it's Added to the Database

muttBunch 120 Reputation points
2023-12-24T08:18:06.3766667+00:00

I would like to exclude two event IDs, 4703 and 5156 so they are not included in the .AddAsync to the Database. We do not need them and there are so many of them in the eventlog. What would be the best/effective way to do that?

The event IDs are coming from

eventInstance.Id

Thanks

using EventLogReader logReader = new EventLogReader(eventsQuery);

for (EventRecord eventInstance = logReader.ReadEvent();
	eventInstance != null;
	eventInstance = logReader.ReadEvent())
{

	var logStore = new LogStore()
	{
    
	    EventId = eventInstance.Id,
    	EventRecord = (int?)eventInstance.RecordId,
	    TimeStamp = (DateTime?)eventInstance.TimeCreated,
    	Description = eventInstance.FormatDescription(),
    	TaskCategory = eventInstance.TaskDisplayName,
    	Level = eventInstance.LevelDisplayName,

	};

	await _applicationDbContext.AddAsync(logStore);
	await _applicationDbContext.SaveChangesAsync(token);

}
Developer technologies | .NET | Entity Framework Core
Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2023-12-24T12:50:19.4366667+00:00

    Write an "if" condition.

        if(eventInstance.Id != 4703 & eventInstance.Id != 5156)
        {
            var logStore = new LogStore()
            {      
                EventId = eventInstance.Id,
                EventRecord = (int?)eventInstance.RecordId,
                TimeStamp = (DateTime?)eventInstance.TimeCreated,
                Description = eventInstance.FormatDescription(),
                TaskCategory = eventInstance.TaskDisplayName,
                Level = eventInstance.LevelDisplayName,
    
            };
            await _applicationDbContext.AddAsync(logStore);
            await _applicationDbContext.SaveChangesAsync(token);
        }
    

    Maybe you can filter with the unknown eventsQuery? Maybe use LINQ's Any() or Where()?

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.