Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

basic linq query

Posted on 2014-09-17
5
Medium Priority
?
207 Views
Last Modified: 2016-02-15
String  PT=String.Empty;
PT = Convert.ToString((from p in Permissions
             where p.AN == AN && p.PC == "OLP"
             select p.PC).first());

Open in new window


In above query, I may or may not get any matching record so in that case  how should I write the above query.

please help me in correcting this.

thanks
meetDinesh
0
Comment
Question by:Dinesh Kumar
[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
5 Comments
 
LVL 2

Expert Comment

by:Akilandeshwari N
ID: 40329696
Check if your query has returned some data, then go for the conversion and other statements. Below is how you do it.
String  PT=String.Empty;
var result = (from p in Permissions
             where p.AN == AN && p.PC == "OLP"
             select p.PC).first();
if(!result.Any() )
{ 
     PT = Convert.ToString(var)
} 

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1200 total points
ID: 40329707
Actually, you'll need to rearrange Akilandeshwari N's suggestion. The reason being, that First will throw an exception if there nothing in the collection. You can use the FirstOrDefault method instead:

String PT = String.Empty;
var query = from p in Permissions
            where p.AN == AN && p.PC == "OLP"
            select p.PC;
var first = query.FirstOrDefault();

if (first != null)
{
    PT = Convert.ToString(first);
}

Open in new window

0
 

Author Comment

by:Dinesh Kumar
ID: 40329868
FirstOrDefault , I am not sure of as there is no default value, please confirm.
0
 
LVL 11

Assisted Solution

by:louisfr
louisfr earned 800 total points
ID: 40330077
Every type has a default value. For string, or any other reference type, it's null.
0
 

Author Closing Comment

by:Dinesh Kumar
ID: 40330130
Thank You very much.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

705 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