C# 从Linq到SQL查询获取结果
我不熟悉LINQtoSQL,不知道在Linq中使用查询结果的正确方法是什么。我使用一个函数返回两个表的数据:C# 从Linq到SQL查询获取结果,c#,linq-to-sql,C#,Linq To Sql,我不熟悉LINQtoSQL,不知道在Linq中使用查询结果的正确方法是什么。我使用一个函数返回两个表的数据: public IQueryable RegresaDatosCredito(int idC) { var credito = from a in context.acreditados where a.creditos.IDCredito == idC select new {
public IQueryable RegresaDatosCredito(int idC)
{
var credito = from a in context.acreditados
where a.creditos.IDCredito == idC
select new
{
Monto = a.Cantidad,
Tasa = a.creditos.TasaInteres,
Plazo = a.creditos.Plazo,
Periodo = a.creditos.Periodo,
Producto = a.creditos.Producto,
Expediente = a.creditos.Expediente
};
return credito;
}
此查询将始终从我的数据库返回一行。然后我想使用这个查询的结果,并在不同的文本框中显示它。在另一个类中,我创建了一个方法来打印上述结果
private void SomeMethod()
{
try
{
var credito = operaciones.RegresaDatosCredito(idCred);
text_MontoC.Text = credito.Monto;
text_TasaC.Text = credito.Tasa;
text_PlazoC.Text = credito.Plazo;
text_PeriodoC.Text = credito.Periodo;
text_ProductoC.Text = credito.Producto;
text_ExpedienteC.Text = credito.Expediente;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
但是我不能访问像credito.
???这样的结果,正确的方法是什么
在RegresaDatosCredito
中,我返回一个IQueryable
数据类型,因为在查询中我使用fk关系连接两个表,我错了吗
谢谢如果您只希望得到一个结果,请使用
Single
方法
var credito = (from a in context.acreditados
where a.creditos.IDCredito == idC
select new
{
Monto = a.Cantidad,
Tasa = a.creditos.TasaInteres,
Plazo = a.creditos.Plazo,
Periodo = a.creditos.Periodo,
Producto = a.creditos.Producto,
Expediente = a.creditos.Expediente
}).Single();
此外,还应该将结果解析为具体类型,而不是anounymus类型
假设您创建了一个类
public class Credito
{
public decimal Monto{get;set;}
public decimal Tasa{get;set;}
public decimal Plazo{get;set;}
public string Periodo{get;set;}
public string Producto{get;set;}
public string Expediente{get;set;}
}
然后你可以像这样使用它
var credito = (from a in context.acreditados
where a.creditos.IDCredito == idC
select new Credito
{
Monto = a.Cantidad,
Tasa = a.creditos.TasaInteres,
Plazo = a.creditos.Plazo,
Periodo = a.creditos.Periodo,
Producto = a.creditos.Producto,
Expediente = a.creditos.Expediente
}).Single();
如果要返回多个,请使用toList()代替Single
然后定义函数以返回
Credito
类型(使用Single()
)或List
(使用ToList()
)如果只需要一个结果,请使用Single
方法
var credito = (from a in context.acreditados
where a.creditos.IDCredito == idC
select new
{
Monto = a.Cantidad,
Tasa = a.creditos.TasaInteres,
Plazo = a.creditos.Plazo,
Periodo = a.creditos.Periodo,
Producto = a.creditos.Producto,
Expediente = a.creditos.Expediente
}).Single();
此外,还应该将结果解析为具体类型,而不是anounymus类型
假设您创建了一个类
public class Credito
{
public decimal Monto{get;set;}
public decimal Tasa{get;set;}
public decimal Plazo{get;set;}
public string Periodo{get;set;}
public string Producto{get;set;}
public string Expediente{get;set;}
}
然后你可以像这样使用它
var credito = (from a in context.acreditados
where a.creditos.IDCredito == idC
select new Credito
{
Monto = a.Cantidad,
Tasa = a.creditos.TasaInteres,
Plazo = a.creditos.Plazo,
Periodo = a.creditos.Periodo,
Producto = a.creditos.Producto,
Expediente = a.creditos.Expediente
}).Single();
如果要返回多个,请使用toList()代替Single
然后定义函数以返回Credito
类型(使用Single()
)或List
(使用ToList()
)您在IQueryable中返回的是“匿名”数据类型,这不是一个好习惯,因为您无法在创建它的方法之外访问它
请尝试声明一个可以保存数据并返回IQueryable的结构,或者甚至-因为您只需要一个值-调用IQueryable上的single(),以获取CreditoStruct并返回该值
public CreditoStruct RegresaDatosCredito(int idC)
{
return (from a in context.acreditados
where a.creditos.IDCredito == idC
select new CreditoStruct
{
Monto = a.Cantidad,
Tasa = a.creditos.TasaInteres,
Plazo = a.creditos.Plazo,
Periodo = a.creditos.Periodo,
Producto = a.creditos.Producto,
Expediente = a.creditos.Expediente
}).Single();
}
您在IQueryable中返回一个“匿名”数据类型,这不是一个好习惯,因为您不能在创建它的方法之外访问它
请尝试声明一个可以保存数据并返回IQueryable的结构,或者甚至-因为您只需要一个值-调用IQueryable上的single(),以获取CreditoStruct并返回该值
public CreditoStruct RegresaDatosCredito(int idC)
{
return (from a in context.acreditados
where a.creditos.IDCredito == idC
select new CreditoStruct
{
Monto = a.Cantidad,
Tasa = a.creditos.TasaInteres,
Plazo = a.creditos.Plazo,
Periodo = a.creditos.Periodo,
Producto = a.creditos.Producto,
Expediente = a.creditos.Expediente
}).Single();
}
既然你返回credito,你应该使用具体类型而不是匿名类型。如果我的查询使用'acreditados'和'creditos'类型,我该如何使用具体类型?我编辑我的问题是,既然你返回credito,你应该使用具体类型而不是匿名类型。如果我的查询使用“acreditados”和“creditos”类型,我该如何使用具体类型?我编辑我的问题是想说谢谢,但我仍然不知道如何使用resultwow,不知道我能做到这一点。谢谢你的帮助,只有一个问题,我能用struct代替class吗?区别是什么?基本上是一样的,但是如果你不知道类和结构的区别,就有了这些区别。谢谢你,但是我仍然不知道如何使用resultwow,我不知道我能做到。谢谢你的帮助,只有一个问题,我能用struct代替class吗?区别是什么?基本上是一样的,但是如果你不知道类和结构的区别,那么就只有这些区别了。谢谢Joachim,非常好的解释谢谢Joachim,非常好的解释