score:154

Accepted answer

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


Related Query

More Query from same tag