如何将 linq var 数据类型传递给方法?

发布于 2024-11-18 11:45:59 字数 1152 浏览 3 评论 0原文

可能的重复:
如何将匿名类型传递给方法?< /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

太傻旳人生 2024-11-25 11:46:00

尝试查看此处:

LINQ :我可以将 var 作为参数传递给方法吗?

或这里:

传递参数

var 只能在本地时使用
变量被声明并初始化
在同一份声明中;变量
不能初始化为 null 或
方法组或匿名函数。

Try look here:

LINQ: Can I pass a var as a parameter to a method?

or here:

Passing the parameter

var can only be used when a local
variable is declared and initialized
in the same statement; the variable
cannot be initialized to null, or to a
method group or an anonymous function.

天气好吗我好吗 2024-11-25 11:46:00

最简单的方法是创建一个具体类型来表示您的结果(上面的 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)

情场扛把子 2024-11-25 11:46:00

使用.net 4中引入的dynamic关键字

DoStuff(new { Message = "Hello Monkey"}); 

static void DoStuff(dynamic args)  
{                  
    Console.WriteLine(args.Message);  
}

Use the dynamic keyword which came in .net 4

DoStuff(new { Message = "Hello Monkey"}); 

static void DoStuff(dynamic args)  
{                  
    Console.WriteLine(args.Message);  
}
西瑶 2024-11-25 11:46:00

所以我解决了它......谢谢我,(很好的解决方案看起来:

System.Collections.IEnumerable 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  };
return i;

然后在数据网格中我只是将 i 作为数据源,绑定它然后瞧!

So I solve it... Thanks to me, (great solution look:

System.Collections.IEnumerable 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  };
return i;

And then in the datagrid I just took i as the datasource, bind it and voilà !!

耀眼的星火 2024-11-25 11:46:00

如果您想要将其传递给您自己的方法,那么根据您想要执行的操作(或者您愿意使用多少反射/约定),泛型可能是您的答案

private string CheckMeOut<T>( T something )
{
    return something.GetType().Name;
}

public void CheckMeOutTest( )
{
    var anon = ( from x in typeof(string).GetMethods( )
                 select new {x.Name, Returns = x.ReturnType.Name} ).First( );
    string s = CheckMeOut( anon );
    Console.Out.WriteLine( s );
}

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

private string CheckMeOut<T>( T something )
{
    return something.GetType().Name;
}

public void CheckMeOutTest( )
{
    var anon = ( from x in typeof(string).GetMethods( )
                 select new {x.Name, Returns = x.ReturnType.Name} ).First( );
    string s = CheckMeOut( anon );
    Console.Out.WriteLine( s );
}
半城柳色半声笛 2024-11-25 11:46:00

如果您使用的是 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.

以酷 2024-11-25 11:45:59

返回这样的东西怎么样:

public class MyType
{
    public ComprobanteRecepcions Recepcions { get;set; }
    public Comprobantes Comprobantes { get;set; }
}

在你的 linq 中

select new MyType { Recepcions  = cr, Comprobantes = c }

What about returning something like this

public class MyType
{
    public ComprobanteRecepcions Recepcions { get;set; }
    public Comprobantes Comprobantes { get;set; }
}

and in your linq:

select new MyType { Recepcions  = cr, Comprobantes = c }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文