Solved

C# linq

Posted on 2013-01-26
4
312 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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