如何将 linq var 数据类型传递给方法?
可能的重复:
如何将匿名类型传递给方法?< /a>
我这里有一个可怕的问题,希望你能帮我解决它
有以下 linq-to-sql 查询,非常简单:
var i = from cr in db.ComprobanteRecepcions join c in db.Comprobantes
on new { cr.RFC, cr.RFCProveedor, cr.Folio, cr.Serie } equals new { c.RFC, c.RFCProveedor, c.Folio, c.Serie }
where
Convert.ToString(cr.IDSucursal) == "4" &&
cr.RFC == "FIN020938SVR "
select new { cr.Serie, cr.Folio, cr.IDStatusComp, c.FechaEmision, c.Comentarios, c.Total };
我想将 i 传递给一个方法,像这样
mymethod void(var a)
当然这是不可能的...并创建一个要返回的类型(类)它,这样的事情
select new MyType {cr.Serie, cr.Folio, cr.IDStatusComp, c.FechaEmision, c.Comentarios, c.Total };
是不切实际的,例如返回 XElement 或 XDocument 那么我还能做什么?我必须用 var i 变量填充数据网格,我不知道如何获取这个变量,我也在 google 上搜索答案,但没有简单的答案...
请理解,我是使用 c#、.net 和 的新手MS Technologies(我是一名 Java 程序员)
Possible Duplicate:
How can I pass an anonymous type to a method?
I have a terrible problem here, hope you can help me solve it
have the following linq-to-sql query, very simple:
var i = from cr in db.ComprobanteRecepcions join c in db.Comprobantes
on new { cr.RFC, cr.RFCProveedor, cr.Folio, cr.Serie } equals new { c.RFC, c.RFCProveedor, c.Folio, c.Serie }
where
Convert.ToString(cr.IDSucursal) == "4" &&
cr.RFC == "FIN020938SVR "
select new { cr.Serie, cr.Folio, cr.IDStatusComp, c.FechaEmision, c.Comentarios, c.Total };
I want to pass i to a method, something like this
mymethod void(var a)
Of course this can't be done... and creating a Type (class) to return it, something like this
select new MyType {cr.Serie, cr.Folio, cr.IDStatusComp, c.FechaEmision, c.Comentarios, c.Total };
is impractical, such as returning a XElement or XDocument so what else can I do ?? I have to fill a datagrid with the var i variable and I don't know how to get this variable, I also googled for answers but there's not and easy one...
Understand that I'm a newbie using c#, .net and MS Technologies (I was a Java programmer)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试查看此处:
LINQ :我可以将 var 作为参数传递给方法吗?
或这里:
传递参数
Try look here:
LINQ: Can I pass a var as a parameter to a method?
or here:
Passing the parameter
最简单的方法是创建一个具体类型来表示您的结果(上面的 MyType),而不是尝试传递匿名类型。
var 实际上只供本地使用,编译器可以根据使用推断类型。一旦将其传递给另一个方法,就必须使用具体类型。
(虽然你可以让你的方法将“object”作为参数,但除了使用反射之外,你将无法用它做太多事情)
The simplest way would be to make a concrete type to represent your results (your MyType above), instead of trying to pass an anonymous type around.
var is really only for local use, where the compiler can infer the types based on use. once you're passing that to another method, you have to use concrete types.
(although you could just make your method take "object" as its parameter, but you won't be able to do much with it after that except using reflection)
使用.net 4中引入的dynamic关键字
Use the dynamic keyword which came in .net 4
所以我解决了它......谢谢我,(很好的解决方案看起来:
然后在数据网格中我只是将 i 作为数据源,绑定它然后瞧!
So I solve it... Thanks to me, (great solution look:
And then in the datagrid I just took i as the datasource, bind it and voilà !!
如果您想要将其传递给您自己的方法,那么根据您想要执行的操作(或者您愿意使用多少反射/约定),泛型可能是您的答案
If the method you want to pass it to is your own, then depending on what you want to do (or how much reflection/convention you are willing to use, generics may be your answer
如果您使用的是 SQL 数据库,您是否考虑过从数据库自动生成数据实体模型 (.edmx)?然后您可以使用那里生成的所有类并避免整个混乱。
If you are using a SQL database, have you considered generating a data entity model (.edmx) from the database automatically? Then you can work with all the classes generated there and avoid this whole mess.
返回这样的东西怎么样:
在你的 linq 中
What about returning something like this
and in your linq: