Solved

basic linq query

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error import assembly 19 31
I need help writing Unit Tests for my ASP.Net MVC application 5 40
Call to SQL server times out 5 56
Easiest intro into .NET CORE? 2 34
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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