score:0
maybe you’re looking for the let
keyword?
var persons =
from _person in people
let placeid =
(
from _peopleplaces in peopleplace
where _person.id == _peopleplaces.personid
select _peopleplaces.placeid
).firstordefault()
let placename =
(
from _places in places
where _places.id == placeid
select _places.name
).firstordefault()
select new
{
id = _person.id,
name = _person.name,
nationality = _person.nationality,
age = _person.age,
placeid = placeid,
placename = placename,
};
edit: as @sambomartin correctly mentioned below, you need to consider what would happen if there is no entry in peopleplace
for a specific person. under the above query, such persons are still returned in the final result, with a placeid
and placename
of null
.
c# allows equality comparison between an int
and a int?
; if the latter is null
, the comparison would always evaluate to false
. thus, when placeid
is null
, no entry from places
would satisfy the _places.id == placeid
condition (assuming the id
field is not nullable), and placename
would evaluate to null
as well.
if you do not want such persons in your final result, you could amend your query by adding where
clauses:
var persons =
from _person in people
let placeid =
(
from _peopleplaces in peopleplace
where _person.id == _peopleplaces.personid
select _peopleplaces.placeid
).firstordefault()
where placeid != null // ensure that the person has an associated place
let placename =
(
from _places in places
where _places.id == placeid
select _places.name
).firstordefault()
where placename != null // ensure that the place appears in the primary table
select new
{
id = _person.id,
name = _person.name,
nationality = _person.nationality,
age = _person.age,
placeid = placeid,
placename = placename,
};
score:3
you cant use the placeid until it's been calculated, i.e. when its enumerated.
there's nothing to stop you querying the three objects in a single expression, e.g.
var people = from p in persons
from ppl in personplaces
from pl in places
where p.id == ppl.personid
&& ppl.placeid == pl.id
select new { name=p.name, placename=pl.name}
sorry for formatting, it's difficult on ipad.
hth
sam
Source: stackoverflow.com
Related Query
- Using Linq in an Anon Type using one of the Anon Types Properties
- Is possible keep the properties type after using linq selection?
- Change the type of one of the objects property and sort it using LINQ
- Convert string[] to int[] in one line of code using LINQ
- The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
- Create a list of one object type from a list of another using Linq
- Take the first five elements and the last five elements from an array by one query using LINQ
- LINQ - 'The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'.'
- The type of one of the expressions in the join clause is incorrect when the types are the same
- The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported
- Left outer join using LINQ -- understanding the code
- How can I write the following code more elegantly using LINQ query syntax?
- Problems trying to use GroupBy with multiple properties using the LINQ Method Syntax
- C# Linq to Entities: The type of one of the expressions in the join clause is incorrect. Type inference failed
- Sort JSON by one or more properties using Dynamic LINQ
- Unable to cast the type X to type Y. LINQ to Entities only supports casting EDM primitive or enumeration types
- Using LINQ to get one entity with the most of a child entity
- Get only one value from the table using LINQ
- Unable to cast the type 'DynamicClass1' to type <T>. LINQ to Entities only supports casting EDM primitive or enumeration types
- This LINQ statement crashes if one of the Properties is NULL. How can I fix this?
- Linq Groupby multiple columns with the same name (An anonymous type cannot have multiple properties with the same name)
- To determine if one of the Strings from a list contains the initial part of a specified string using LINQ
- code first approach error: the specified type member 'yyyxx' is not supported in linq to entities
- How to write this code using the Linq Extension Method-Syntax?
- Build a dictionary of objects and append text to one of its properties using Linq
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Unable to cast the type 'x' to type 'y'. LINQ to Entities only supports casting EDM primitive or enumeration types
- Parse XML using LINQ and fill existing object's properties rather than creating a new one
- LINQ entity data model generated code error - The type 'DBContexts.Category' already contains a definition for 'ID'
More Query from same tag
- ListBoxView populated using Linq based on TimePicker Value
- LINQ Query that is able to identity a Row in a Junction Table with two variables - How to do it?
- MVC: Display an input if two rows match in database using LINQ
- How to calculate rank from list of integer?
- Equivalent of the following SQL Count to LINQ Count
- How to compare two DataTables with common values
- Need help in converting SQL query to LINQ
- LINQ SUM Nullable
- Getting minimum date from a list of records with date columns
- C# Return match found with Contains
- Compare 2 Datatables to find difference/accuracy between the columns
- How to Remove specific attributes in XMLDocument?
- Performance LINQ async await
- Using LINQ to get distinct items that do not join
- Linq Contains() Not supported
- Is there any way to add 'System.StringComparison.OrdinalIgnoreCase' in startsWith in Where clause in linq C#
- Split array in array of array with LINQ
- Writing to XML using XDocument, but knowing where to write
- Nested Select in C# Linq
- Select via Linq and EF with empty list for linked tables
- System.Linq.Dynamic does not support OrderByDescending("someColumn")?
- The data source does not support server-side data paging
- How to include sorted navigation properties with Entity Framework
- EF4 exception with relationship
- how to update many to many table in entity framework
- LINQ select more fields of same type
- How to write Linq query to check whether List have that id
- Select records with max property value per group
- Writing to xml from C#
- How to sort collection of file paths using c# linq