score:1
Accepted answer
var result = (from a in allteams
join m in modifiedteams on a.id equals m.id into mj
from m in mj.defaultifempty()
select new [] { m?.driver?.driverid ?? a.driver?.driverid,
m?.codriver?.driverid ?? a.codriver?.driverid
}).selectmany(i => i).tolist();
score:0
public static void kvyq()
{
list<team> modifiedteams = new list<team>() {
new team() {
id="t1"
,driver=new driver() {
driverid="d2"
}
,codriver=new driver() {
driverid="c1"
}
}
,new team() {
id="t2"
,driver=new driver() {
driverid="d1"
}
}
};
list<team> allteams = new list<team>() {
new team() {
id="t1"
,driver=new driver() {
driverid="d1"
}
,codriver=new driver() {
driverid="c1"
}
}
,new team() {
id="t2"
,driver=new driver() {
driverid="d2"
}
,codriver=new driver() {
driverid="c2"
}
}
};
var driverdetails = modifiedteams.select(x => new team()
{
driver = x.driver,
codriver = x.codriver
});
foreach (var item in driverdetails)
{
driver d = (item.driver == null) ? allteams.select(x => x.driver).firstordefault() : item.driver;
driver c = (item.codriver == null) ? allteams.select(x => x.codriver).firstordefault() : item.codriver;
console.writeline(d.driverid + " " + c.driverid);
//{d2,c1}
//{d1,c1}
}
}
score:0
public static ienumerable<string> toids(this ienumerable<string> teamids,
ienumerable<team> modifiedteams, ienumerable<team> allteams)
{
var modifiedteamslookup = modifiedteams.todictionary(team => team.id);
var allteamslookup = allteams.todictionary(team => team.id);
return teamids
.selectmany(teamid => teamid.todriverids(modifiedteamslookup,
allteamslookup);
}
Source: stackoverflow.com
Related Query
- Linq query to Join tables if value not in first table fetch from second
- Linq query to join two tables and return object from first table - PagedList used
- Linq : Join tables take from table whose value is not null
- linq query to join two tables and get the count from one table values from the other
- linq join query get single record from second table
- LINQ Query To Join Two Tables and Select Most Recent Records from Table B corresponding to Table A
- Linq join get latest value from second table
- Linq query to select data from table and join 2 tables
- How to pull one column from second table in Linq query with join
- Joining two tables with LINQ while also returning null records from the second table
- Which LINQ query to select rows from 1 table that are not in another table
- LINQ Join not returning results if second or third table empty
- How to join two table from two different edmx using linq query
- LINQ Join / Union two lists of objects taking precedence from second over first
- Linq query where first column from DataTable is compared to second column for duplicates
- Get first value from table with where either have the value match or null with LINQ
- Check a value from a second table within a LINQ Statement
- Why does my LINQ to SQL query fail the first time ("Row Not Found or Changed") and succeed the second time?
- LINQ to SQL join two tables to select parent table twice based on two different columns from child table
- Linq query from 3 tables using Joiner table
- Linq - Inner Join not retrieving all records from left table
- Linq query selects values from first row only in table
- assigning value from select linq query not updating the list
- Comparing a list of CSV values in Linq to match those from second list with any value in first list
- How to get record form a different table based on a value from first table with linq expression?
- Linq C# Join tables if value of two fields exists in another table
- linq join query for getting one record from 2nd table
- Linq Query join a table to a List<string> object to find any Strings not in table
- How can I make a JOIN query with LINQ between two tables (entities) with many-to-many relationship, while there is no joining table in EF?
- Doing an LEFT JOIN in LINQ to Entities erroring when trying to get data from second table
More Query from same tag
- convert nested for loop to linq with condition
- How does nesting an OrderBy in a Dynamic Linq query work?
- Linq complex object groupBy
- IQueryable where clause overriden in inherited class
- multiple records with the same order id incremented from the previous MAX value
- Mongodb c# driver: view MQL bson query generated from linq
- Why do the nullable explicit cast LINQ operators throw invalid format exceptions on empty values?
- how to use sum function with were condition and isnull check in linq to entity?
- How can I get an XElement's .InnerText value in Linq to XML?
- EF7 Getting null values for entity's collection of entities which are many to many
- Sequence contains no matching element, FirstOrDefault
- How to check available hotel rooms
- Nested Linq query returns three times the result?
- NOT IN sub select from T SQL to LINQ?
- Putting multiple values in a WHERE clause in Azure Mobile Services
- Requiring users to update .NET
- NullReference exception when using LINQ Contains with Entity Framework
- prevent duplicates items listview form listbox asp.net mvc3
- Select records count from multiple tables in a single query
- Getting odd/even part of a sequence with LINQ
- What expression method to use to get back parameter?
- Linq - Updating dynamic column
- LINQ query returns old results when source list is re-initialized
- c# Razor Pages Select Tag Helper
- Return results from data table in a sequence using linq
- Dynamic Linq SearchFor from entities by Passing Tablename,ColumnName and value as parameter
- How to assert a Linq collection with unit test
- NHibernate 3 and LINQ support?
- How to use Linq to check for exact match?
- Linq (GroupBy and Sum) over List<List<string>>