score:0
Project out the property you want with .Select(x => x.MyProp);
return fatcaQuestionaires.Select(x => x.Questionaire).ToList();
score:6
Using Linq, first you can do a Where
filtering the desired rows, and then Select
to projecting only Questionaire properties.
Try this
return context.FatcaQuestionaires
.Where(p => p.ContactID == contactId)
.Select(p => p.Questionaire)
.ToList();
score:4
You almost have it. Select
invokes a transformation function, so it is just making a list of bool
. You need a Where
clause to do the filtering, and then a Select
.
var fatcaQuestionaires = context.FatcaQuestionaires
.Where(p => p.ContactID == contactId)
.Select(p => p.Quentionaire);
return fatcaQuestionaires.ToList();
score:1
Change Select
to Where
as I mentioned in my comment. Select
will just return a bool value for every entry's evaluation base on your lambda expression.
So you end up with a List<bool>
var fatcaQuestionaires = context.FatcaQuestionaires
.Where(p => p.ContactID == contactId)
.Select(q=>q.Questionaire).ToList();
return fatcaQuestionaires;
score:2
What you have written looks like it will return a list of booleans and not compile. What you need is a combination of a where
clause and a select
clause.
return context.FatcaQuestionaires
.Where(p => p.ContactID == contactId)
.Select(p => p.Questionaire).ToList();
Where()
is what limits the FactaQuesntionaires, and Select()
is where you choose the property to return. You can also write it like this:
return (from p in context.FatcaQuestionaires
where p.ContactID == contactId
select p.Questionaire).ToList();
Source: stackoverflow.com
Related Articles
- List or Array of String Contain specific word in Html Source Code
- Return list of string
- Linq to NHibernate return entities whose name starts with contents of string list
- linq query to return a string from a list
- Return a list of string using LINQ through a loop of conditionals
- Return a list of Directory (Folder) names that match a partial string
- Return the number of times a string occurs as a property in a list of custom objects
- LINQ function to return list but compiler says function doesn't return a value on all code path
- How to convert a list of anonymous object to string array using linq for return using Json Result in ASP.NET MVC
- Return list of string from list nested in a list
- C# Return true if string is different from any of the string in a list of strings using LINQ
- c# Linq or code to extract groups from a single list of source data
- linq return all items where string field is like any in a list of strings
- C# Linq to check if the given list of string is available in either (oldNumber and newNumber) of the Column and return only the matched cell value
- Match a comma delimited string to an integer list using linq and return the new objects
- Check if list contains element that contains a string and get that element
- How to check if all list items have the same value and return it, or return an “otherValue” if they don’t?
- LINQ query to return distinct field values from list of objects
- C# - code to order by a property using the property name as a string
- Using Linq to return a Comma separated string
- Return list using select new in LINQ
- Return list of specific property of object using linq
- Is there a LINQ function for getting the longest string in a list of strings?
- C# Select elements in list as List of string
- compare two list and return not matching items using linq
- Join together all items of a list in an output string in .NET
- LINQ return items in a List that matches any Names (string) in another list
- Using LINQ to convert a list to a CSV string
- How to return empty string if an object is null?
- Replace string values in list
- Storing database data in a dictionary to display on a chart
- How to bind boolean to combo box
- slicing array based on selection masks
- LINQ execute SQL query with output parameter
- How to turn complex sql into linq
- How do I use Any() instead of RemoveAll() to exclude list items?
- How to transfer data when using LINQ as DAL?
- How do i insert data in a row based on whether a column is correct?
- How to sort and output data stored in a list
- C# Linq getting value of descendants
- WP7 Return the last 7 days of data from an xml web service
- Nhibernate generating OUTER JOIN for a fetch
- How can I use conditional Order with EntityFramework/linq
- How do I compare the old data type value with the new ones
- C# Lambda - get distinct Id and append other field value
- How to read data from XNode
- Issue with the string value "Boolean" using HtmlHelper DisplayFor
- C# Find and edit single object on list
- faster take n record in linq
- XML mapping file for many-to-many relation with NHIbernate