Home > OS >  How to change values inside IQueryable (not database)
How to change values inside IQueryable (not database)

Time:01-08

Question: How to change values inside IQueryable? (dont change inside database)

Query has data from database.

IQueryable<My_Model> Query = from x in _context.My_DbSet
                              select x;
my_List = await Query.AsNoTracking().ToListAsync();

Inside Query, I want to change some values. for example: if name is empty than display "Missing Name".

If location is "NYC" than display "NEW York City"

Database table / Model class

|------|----------|
| Name | Location | 
|------|----------|
| Dave | NYC      |
|------|----------|
|      | NYC      |
|------|----------|
| Bob  | LA       |
|------|----------|

CodePudding user response:

The following code applies case when on the sql side instead of you while fetching data

_context.My_DbSet.Select(x => new My_DbSet
{
  Name = x.Name ?? "Missing Name"
});

old EF :)

(from data in _context.My_DbSet
                         select new My_DbSet
                         {
                             Name = data.Name ?? "Missing Name"
                         }).FirstOrDefault();
  • Related