Solved

basic linq query

Posted on 2014-09-17
5
158 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 74

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now