Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Could you please help me that how to check more than 1 values to check in c# lamda expression

DocumentationLink = context.APIDocumentationTypes.Where(x => x.DocumentationTypeId == a.DocumentationTypeId).Select(x => x.DocumentationDescription).FirstOrDefault().ToLower() != "value1" ? a.DocumentationLink : "",


i need to check with
value1
and
value2
in same line.

What I have tried:

<pre>DocumentationLink = context.APIDocumentationTypes.Where(x => x.DocumentationTypeId == a.DocumentationTypeId).Select(x => x.DocumentationDescription).FirstOrDefault().ToLower() != "value1" && "value2"? a.DocumentationLink : "",
Posted
Updated 14-Aug-17 3:09am

Try:
C#
string result = context.APIDocumentationTypes.Where(x => x.DocumentationTypeId == a.DocumentationTypeId)
                                             .Select(x => x.DocumentationDescription)
                                             .FirstOrDefault().ToLower();
DocumentAtionLink = ( result != "value1" && result != "value2") ? a.DocumentationLink : "";
But do note that FirstOrDefault will return null if there are no elements that matched your Select - at which point your ToLower call will cause a NullReferenceException.
 
Share this answer
 
v2
Comments
BillWoodruff 14-Aug-17 9:56am    
Hi, I keep seeing that call to'FirstOrDefault without a predicate followed by 'ToLower as a guaranteed error. I miss something ?
OriginalGriff 14-Aug-17 10:27am    
FirstOrDefualt doesn't need a predicate / lambda expression - it just returns the first of a enumerable sequence, or null if the collection is empty.
Since the Select that precedes it returns an IEnumerable string collection (or it couldn't be compared in his original code with a string) you can call ToLower on it.
Me, I'd omit the ToLower until after I'd checked the null return, but your solution of using it inside the Select lambda is just as efficient (as the whole sequence isn't evaluated with a FirstOrDefault, so it doesn't generate unwanted strings for each element).

[edit]FirstOrDefualt can take a lambda. of course.[/edit]
OriginalGriff 14-Aug-17 10:29am    
:doh: Brain death is setting in - FirstOrDefault *can* accept a predicate, of course it can. :sigh:
BillWoodruff 14-Aug-17 12:25pm    
My brain damage is greater than your brain damage, and, I've got years of mileage on my neurons to prove it :)

My impression is that the OP wants to iterate the matches for x.DocumentationTypeId == a.DocumentationTypeId).Select(x => x.DocumentationDescription and then find the first one in that IEnumerable<ocumentationdescription> that doesn't match on "value1" and "value2"
Here's a guess at what you might need to do:
private DocumentationLink getadl(AContext ctx, APIDocumentationTypes a)
{
    var adl = ctx.APIDocumentationTypes.Where(x => x.DocumentationTypeId == a.DocumentationTypeId)

        .Select(x => x.DocumentationDescription.ToLower())

        .FirstOrDefault(
            descr => !(descr == "value1" || descr == "value2")
    );

    // what is the default Type here if no match is found ?
    return (adl == "") ? null : a.DocumentationLink;
}
Of course, you can create "intermediate" variables in a 'Select query, if necessary/
 
Share this answer
 
Comments
DGKumar 16-Aug-17 4:32am    
Thank you for your valuable suggestion.

I need to include result value on property like below.
result = from tbl in Data1
select (new APIProp {
DocumID=tbl.ID,
DocumentationLink = context.APIDocs.Where(x => x.DocuId == tbl.DocuId).Select(x => x.Description).FirstOrDefault().ToLower() != "Value1" ? tbl.DocumentationLink : ""
}).Where(x => x.DocumentationId == docID).ToList();

with more than 1 value checking for same property

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900