Solved

C# linq

Posted on 2013-01-26
4
311 Views
Last Modified: 2013-01-27
Below is the linq I am using to populate a datagridview. The PassFailFlag in the database is either 0 or 1 - representing fail or pass. I don't want the datagridview to display 0 and 1. I would like it to display the text "Fail" or "Pass".

public enum PFType { Fail, Pass };

dataGridViewSearch.DataSource = (from ph in DB.PanelHistories
                                                     join u in DB.Users on ph.UserID equals u.ID
                                                     where ph.UserID == (int)cboSearchOperator.SelectedValue
                                                     select new
                                                     {
                                                         ph.PartNumber,
                                                         ph.OrderNumber,
                                                         ph.PieceNumber,
                                                         ph.MeasurementDate,
                                                         (PFType) ph.PassFailFlag,
                                                         ph.MeasuredPOA,
                                                         ph.TestType,
                                                         u.UserName
                                                     });

The above doesn't work. I get the message "invalid anonymous type declarator".

What is the easiest way to get the Pass/Fail column in my datagridview to display the text "Fail" and "Pass" instead of 0 and 1?

Thanks!
0
Comment
Question by:neekolzun
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
Ryan McCauley earned 500 total points
ID: 38823578
You can't map the LINQ directly to an Enum like that, but you can use a case statement. Replace the line that says:

(PFType) ph.PassFailFlag,

Open in new window


with the following:

PassFailFlag = 
        (
            ph.PassFailFlag == 0 ? "Fail" :
            ph.PassFailFlag == 1 ? "Pass" :
            "Other"
        )

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38824003
or simply:

((PFType)ph.PassFailFlag).ToString()

Open in new window

0
 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 38824734
Good call, sedgwick - I always forget that you can just .ToString the boolean type to get its name.
0
 

Author Comment

by:neekolzun
ID: 38824749
Just what I needed! Thank you so much!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question