score:154
while the answer by octavioccl works, it's better to first project the query result into anonymous type, and then switch to enumerable and convert it to tuple. this way your query will retrieve from the data base only the fields needed.
codes = codesrepo.searchfor(predicate)
.select(c => new { c.id, c.flag })
.asenumerable()
.select(c => new tuple<string, byte>(c.id, c.flag))
.tolist();
note: the above rule applies to ef6. ef core naturally supports tuples (in projection or as join/group keys) via tuple constructor, e.g. the original query simply works
codes = codesrepo.searchfor(predicate)
.select(c => new tuple<string, byte>(c.id, c.flag))
.tolist();
but not the tuple.create
method (ef core 2.x).
score:0
just my two cents: this has caught me out a few times with the type names:
a few noddy examples:
private tuple<string, byte> v1()
{
return new tuple<string, byte>("", 1);
}
private (string, int) v2()
{
return ("", 1);
}
private (string id, byte flag) v3()
{
return ("", 1);
}
regards.
score:1
use this method to do this and use the async.
var codes = await codesrepo.searchfor(predicate)
.select(s => new
{
id = s.id,
flag = s.flag
}).firstordefaultasync();
var return_value = new tuple<string, byte>(codes.id, codes.flag);
score:10
in linq to entities you can project onto an anonymous type or onto a dto.to avoid that issue you can use asenumerable
extension method:
codes = codesrepo.searchfor(predicate).asenumerable().
.select(c => new tuple<string, byte>(c.id, c.flag))
.tolist();
this method lets you work with linq to object instead linq to entities, so after call it,you can project the result of your query in whatever you need.the advantage of using asenumerable
instead tolist
is that asenumerable
does not execute the query, it preserves deferred execution. it's good idea always filter your data first before call one of these methods.
score:12
try this:
codes = codesrepo.searchfor(predicate)
.select(c => tuple.create(c.id, c.flag))
.tolist();
been informed this isn't accepting in linq to entities.
another option would be to pull the result into memory before selecting. if you are going to do this i would recommend doing all of the filtering before the .asenumerable() as it means you are only pulling back results that you want as opposed to pulling back the whole table and then filtering.
codes = codesrepo.searchfor(predicate).asenumerable()
.select(c => tuple.create(c.id, c.flag))
.tolist();
as well tuple.create(c.id, c.flag) could be changed to new tuple(c.id, c.flag) if you want to make the code a bit more explicit in the tuples types
score:69
just an updated answer for c# 7, now you can use a simpler syntax to create valuetuples.
codes = codesrepo.searchfor(predicate)
.select(c => new { c.id, c.flag })
.asenumerable()
.select(c => (c.id, c.flag))
.tolist();
you can even name the properties of the tuple now:
codes = codesrepo.searchfor(predicate)
.select(c => new { c.id, c.flag }) // anonymous type
.asenumerable()
.select(c => (id: c.id, flag: c.flag)) // valuetuple
.tolist();
so instead of using it as item1 or item2 you can access it as id or flag.
more docs on choosing-between-anonymous-and-tuple
Source: stackoverflow.com
Related Query
- Create a Tuple in a Linq Select
- Linq code to select one item
- How to create LINQ Expression Tree to select an anonymous type
- How to create a dynamic LINQ select projection function from a string[] of names?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ Source Code Available
- C# Create object with dynamic properties : LINQ select List<object> values by property names array
- Create a Dynamic Linq to EF Expression to Select IQueryable into new class and assign properties
- creating Linq to sqlite dbml from DbLinq source code
- Using Linq Select to create a new inherited object
- How to create dynamic Linq Select Expression with anonymous objects
- Avoiding duplicate code in Linq Select method
- Code Rewite for tuple and if else statements by using LINQ
- Linq to entities use `Func` to create property in a select statement that produces an anonymous object
- How to Select top (5) contributors group by Business type code in c# linq query
- How do I create a variable Select Statement in LINQ
- source code for LINQ 101 samples
- How to write aggregate query in LINQ reusing part of the select code
- How do you create a dynamic select projection from a generic using Linq in C#?
- Create DataTable using LINQ to select multiple columns
- Create dynamic LINQ expression for Select with FirstOrDefault inside
- how to select data by linq in many-to-many relationship in First code Entity framework 5
- How to dynamically create linq code at runtime and get results
- create new object within C# Linq syntax on Select
- How to write C# LINQ code to select based on condition
- Using Linq Select to create a list that contains more items than the original list
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- linq create index with select
- LINQ Select mutiply columns as data source for combobox C#
- How do I create a linq query where the select column name is a variable
More Query from same tag
- Get a count of elements that appear once in a list with LINQ
- Can someone explain to me how this LINQ/Entity Framework query actually works?
- Using Linq to shape structure for Json serialization
- How to handle NULL object property with FirstOrDefault using Linq
- Linq: how to group by many-to-many relationship?
- Building Entity Framework Linq queries with variables
- I have loaded Excel files into .NET using LINQ, now how to bulk insert data into DB (oracle) tables?
- ASP.NET MVC and Linq, when to use?
- Is It Possible to do this T-SQL Query with LINQ (to Entities)
- Formatting Dates in LINQ query when using GroupBy
- Create a dictionary on a list with grouping
- Help With Generic LINQ OrderBy Lambda Expression
- Create a entity framework LINQ group join query to return a subset of properties from DTO and a list of another DTO, ASP.net core
- LINQ to SQL GroupBy Max() lambda
- Filter on a collection using a List containing arguments
- C# - list sorting - x and y
- create XML file for each record of the DataTable using Linq C#
- How to perform group by operation in dataTable using multiple columns dynamically in c#?
- NullReferenceException when Joining 2 Column with Same ID
- Repeat LINQ Query
- Get Max Date from Linq
- Linq query to return top two results when there is some conflict, is not working?
- Joining two datatables as parent-child in linq
- Searching a list of objects for a non empty property
- Group by with select new array using linq
- Chart of IEnumerable LINQ equivalents in Scala?
- Multiple Left Join and Inner Join with LinQ
- c#- select keys and values that does not exist in another dictionary
- Filter content for each item in list coming from AJAX
- C#, Entity Framework, How to Update(delete, add, edit) multiple rows at same time?