[英]Get Fields From not typed <Ienumerable>Object c# linq
I want a method in my program that returns the resulting of a linq Query, here is the function : 我想要程序中的一个方法返回linq Query的结果,这是函数:
public static IEnumerable<object> ConsultasSQL(int TipoConsulta)
{
SQLDataContext DC = new SQLDataContext();
IEnumerable<object> consulta = (dynamic)null;
switch(TipoConsulta)
{
case 1: // Registro en Linea
DC = SQLDataContext.GetDataContext("Tiendas", componerCS());
consulta = from tienda in DC.Tiendas
where tienda.Cod__Tienda == Globales.Tienda
select tienda;
break;
case 2:// Efectivo_Local
DC = SQLDataContext.GetDataContext("Formas de Pago",componerCS());
consulta = from pagos in DC.Formas_de_Pago
where pagos.Efectivo_Local == 1
select pagos;
break;
case 3: // Productos
DC = SQLDataContext.GetDataContext("Item",componerCS());
consulta = from Productos in DC.Item
select Productos;
break;
}
return consulta.ToList();
}
I called this function in other place of my program like this : 我在程序的其他位置调用了此函数,如下所示:
public static void Efectivo_local()
{
var consulta = ConsultasSQL(3);
// Globales.IdPagoLocal = consulta.First().ID_Pago;
//Globales.DesriPagoLocal = consulta.First().ID_Pago;
}
The code works and if I put consulta as a Datagridview datasource it shows me the data , but I have two problems : 该代码有效,如果我将Consulta用作Datagridview数据源,它将向我显示数据,但是我有两个问题:
1 - if I put datagridview1.datasource = consulta.first() --> Its show me nothing :( 2 - I dont know how to retrieve the value of specific field of consulta.tolist() 1-如果我把datagridview1.datasource = Consulta.first()->它什么也没给我显示:( 2-我不知道如何检索Consulta.tolist()特定字段的值
As you can see in the picture below , consulta have 144 records and i want to retrieve the field 1 of the second row , its like and array? 正如您在下图中所看到的,Consulta有144条记录,我想检索第二行的字段1,其像和数组吗? [1,1] ??
[1,1]?
http://i.imgur.com/5xHe8Hi.jpg ) http://i.imgur.com/5xHe8Hi.jpg )
You could add an interface to your data classes. 您可以将接口添加到数据类。 That said, if you know the type you're expecting to get at runtime, why are you using
IEnumerable<object>
at all? 也就是说,如果您知道期望在运行时获得的类型,那么为什么要使用
IEnumerable<object>
? Why not have three separate functions, each of which has a single defined purpose? 为什么不具有三个单独的功能,每个功能都有一个定义的用途? It would be better OO design, imo.
更好的OO设计,imo。
To get specific value, you have to do casts of value, so change SQLDataContext: 要获取特定值,必须进行值转换,因此更改SQLDataContext:
private SQLDataContext DC = new SQLDataContext();
...
public static IEnumerable<object> ConsultasSQL(int TipoConsulta)
{
...
}
And add this code: 并添加以下代码:
var consulta = ConsultasSQL(3) as List<DC.Item.Productos>;
if (consulta != null) {
datagridview1.datasource = consulta.first();
Globales.IdPagoLocal = consulta.Find(c => c.id == 123456).ID_Pago;
}
But I suggest you change the code as Jeff Koch says... :-) 但是我建议您像Jeff Koch所说的那样更改代码... :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.