Solved

basic linq query

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 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