Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anonymous type to value tuple casting #22889

Open
mojtabakaviani opened this issue Oct 27, 2017 · 4 comments
Open

Anonymous type to value tuple casting #22889

mojtabakaviani opened this issue Oct 27, 2017 · 4 comments

Comments

@mojtabakaviani
Copy link

With many improvments for value tuple type, now if can casting anonymous type to tuple. a big change in MVC when pass model to view. for example:
return model:

var news = from n in db.News 
           select (n.Id, n.Title, CreatedBy = n.User.Name);
return View(news);

in view:

@model IEnumerable<(int Id, string Title, string CreatedBy)>

but before is must solved #12897

@jcouv
Copy link
Member

jcouv commented Oct 29, 2017

I'm sorry, I didn't understand the issue.
Does the code you included in your description work? If not, what error are you getting?
If it's possible, could you describe the problem with pure C# (no MVC)? That would help me understand the problem.
Thanks

@alrz
Copy link
Contributor

alrz commented Oct 29, 2017

@mojtabakaviani I think this is a duplicate of #12897 as you mentioned. The example you gave wouldn't be possible without #12897 (and the corresponding feature in EF (or any other LINQ provider)).

@mojtabakaviani
Copy link
Author

Yes, anonymous types just access in definition block and can not use in deferent code block. #12897 issue is expression trees not support for tuples, but in want can cast anonymous type to tuples.

var news1 = from n in db.News 
           select new { n.Id, n.Title, CreatedBy = n.User.Name };
//casting anonymous to tuple
var news2 = news1 as IEnumerable<(int Id, string Title, string CreatedBy)>;

in this simple casting anonymous to tuples and can send to other code block.

@alrz
Copy link
Contributor

alrz commented Oct 29, 2017

no type equivalency is defined between a value type and anonymous types. even if we had support for duck typing, that wouldn't work because tuple names are not present in the type itself, (just as metadata).

@jcouv jcouv added this to the Unknown milestone Nov 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants