【问题标题】:Get result from Linq-to-SQL query从 Linq-to-SQL 查询中获取结果
【发布时间】:2012-01-12 19:17:35
【问题描述】:

我是 Linq-to-SQL 的新手,我不知道在 Linq 中使用查询结果的正确方法是什么。我使用了一个返回两个表数据的函数:

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 关系连接两个表,我错了吗?

谢谢

【问题讨论】:

  • 由于您要返还贷方,因此您应该使用具体类型而不是匿名类型。
  • 如果我的查询使用 'acreditados' 和 'creditos' 类型,我该如何使用具体类型?
  • 我正在编辑我的问题以说明这一点

标签: c# linq-to-sql


【解决方案1】:

如果您只期望一个结果,请使用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();

您还应该将结果解析为具体类型而不是匿名类型

假设你创建了一个类

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();

如果你想返回它们中的各种使用而不是 Single,toList() 就是这样。

然后定义您的函数以返回 Credito 类型(带有 Single())或 List<Credito>(带有 ToList()

【讨论】:

  • 谢谢你,但我仍然不知道如何使用结果
  • 哇,不知道我能做到。感谢您的帮助,只有一个问题,我可以使用结构代替类吗?有什么区别?
  • 如果您不了解类和结构的差异,基本上它会是相同的,但只是有这种差异msdn.microsoft.com/en-us/library/aa664471(v=vs.71).aspx
【解决方案2】:

您在 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();
}

【讨论】:

  • 谢谢 Joachim,很好的解释
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
相关资源
最近更新 更多