简体   繁体   中英

C# change linq list (int) item to (string)

I have been tasked with fixing some issues in a C# application someone else made (someone left the company) and I am not really a qualified C# programmer its only a hobby so I am a bit stuck with the following.

var JobQuery = (from datavalue in dc.JobInformations
    where datavalue.JobOpen != 20
    select new
    {
    datavalue.JobID,
    datavalue.CompanyName,
    datavalue.Contact,
    datavalue.Date,
    datavalue.HireOrSale,
    datavalue.JobOpen,
    datavalue.Notes
    }
    );

var JobList = JobQuery.ToList();

Anyway I need to take the list and loop through it which I can do using something like this:

foreach (var item in JobList){
    if (item.HireOrSale = 1)    {
        item.HireOrSale = "Sale";
    }   
    yield return item;
}

As you can see what I am trying to do is change the Hire or Sale which is stored as an INT to a string for better reading on the front end, I can not convert an string to int which is where I am getting stuck. Is there a way I can say build another list in the loop to allow me to define the data type for each in case its different? or is there a way to change the items data type directly?

Used:

            var JobList = (from datavalue in dc.JobInformations
                           where datavalue.JobOpen == 1
                           select new
                           {
                               JobID = datavalue.JobID,
                               CompanyName = datavalue.CompanyName,
                               Contact = datavalue.Contact,
                               Date = datavalue.Date.ToShortDateString(),
                               HireOrSale = datavalue.HireOrSale == 2 ? "Service" :
                               datavalue.HireOrSale == 0 ? "Hire" :
                               datavalue.HireOrSale == 1 ? "Sale" : "",
                               JobOpen = datavalue.JobOpen == 1 ? "Awaiting Quote" :
                               datavalue.JobOpen == 2 ? "Quoted" :
                               datavalue.JobOpen == 3 ? "PO Recieved" :
                               datavalue.JobOpen == 4 ? "Goods On Order" :
                               datavalue.JobOpen == 5 ? "Goods Delivered" :
                               datavalue.JobOpen == 6 ? "Job Complete" :
                               datavalue.JobOpen == 20 ? "Job Cancelled" : "",
                               Notes = datavalue.Notes
                           }
                           );
var JobQuery = (from datavalue in dc.JobInformations
    where datavalue.JobOpen != 20
    select new
    {
       JobID = datavalue.JobID,
       CompanyName = datavalue.CompanyName,
       Contact = datavalue.Contact,
       Date = datavalue.Date,
       HireOrSale = datavalue.HireOrSale==1?"Sale":"",//if not 1, use your default value
       JobOpen = datavalue.JobOpen,
       Notes = datavalue.Notes
    }
    );

var JobList = JobQuery.ToList();

To expand on Tim's comment on your question, this might be a very good use for an enum if you have multiple options, each mapped to a number:

public enum HireOrSale
{
    None = 0,
    Sale = 1,
    SomethingElse = 2,
    MoreStuff = 3
}

Then in your LINQ statement you can do HireOrSale = (HireOrSale)datavalue.HireOrSale to convert the int to the correct enum value.

When it comes time to display it, you can just called ToString and it will print the name of enum . For example, the short segment of code below will print "Sale".

int input = 1;
HireOrSale enumValue= (HireOrSale)input;
String displayValue = enumValue.ToString();

The correct solution, is to create another object, with only the information needed for the front end (A Viewmodel), which will only contain the fields needed to be shown of JobInformation:

An example would be...

JobInfoViewModel

String HireOrSale int JobId DateTime date

and then you would create a List and work with that!

This way, you only pass to the front end the information needed, no more, no less!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM