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 Articles
- Using Linq in an Anon Type using one of the Anon Types Properties
- Is possible keep the properties type after using linq selection?
- 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
- Using LINQ to find duplicates across multiple properties
- Find first element of certain type in a list using LINQ
- Using LINQ to group by multiple properties and sum
- How can anonymous types be created using LINQ with lambda syntax?
- C# re-use LINQ expression for different properties with same type
- Find all child controls of specific type using Enumerable.OfType<T>() or LINQ
- group by using anonymous type in Linq
- Using Linq to Select properties of class to return IEnumerable<T>
- The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported
- Group items in a list based on two properties using LINQ
- Entity Framework Linq Query to List - Error when using contains: Only primitive types, enumeration types and entity types are supported
- using LINQ how can i concatenate string properties from itesm in a collection
- Using LINQ expression to retrieve IEnumerable of properties from IEnumerable of POCO
- using out type in linq
- Merging 2 lists and sum several properties using LINQ
- Left outer join using LINQ -- understanding the code
- How to reuse a linq expression for 'Where' when using multiple source tables
- How to check if all items belong to same type using LINQ
- Avoiding code repetition when using LINQ
- Iterating over class properties using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- Using strict types in linq grouping query
- Using a type alias in a linq statement is generating an error
- LINQ Source Code Available
- C# Linq Order a list with a reference list
- Linq filtering in a property's getter
- What are some clever uses of LINQ?
- DB Context - Query to select post from a category
- Linq Concat to Lazy-Load it's contents
- Gridview always add new row when click button?
- Get previous day data from database based on frequently changing date on button click
- Linq query syntax
- Refactor select part of Linq expression?
- Replacing loops with linq code
- Entity Framework with Oracle using odp.net not taking parameters in linq query
- Why does Linq do joins differently
- LINQ count character apperance
- Linq preserve and filter collection based on inner collection
- how to Order a CollectionA using ListB ordering it in order of ListB Items in VB.net
- Can I use a CAST inside a LINQ to Entities query?
- How to await (in async method) a linq request containg a c# method call
- LINQ to Entities does not recognize the method 'Int32 IndexOf(Int32)'
- assign data from LINQ to ArrayList
- Nested LINQ query using Contains with a bigint