Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change values inside IQueryable (not database)

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".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Database table / Model class

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

>Solution :

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading