Solved

basic linq query

Posted on 2014-09-17
5
173 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
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 300 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 200 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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