Solved

C# linq

Posted on 2013-01-26
4
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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