Reputation: 2336
I'm new to Linq-to-SQL and I don't what is the correct way to use results from a query in Linq. I use a function which returns data of two tables:
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;
}
This query will always return one row from my database. Then I want to use the result from this query and show this in different textBoxes. In other class I created a method to print this result like I mentioned above.
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());
}
}
But I can't access results doing something like credito.
???, what is the correct way to do it?
In RegresaDatosCredito
I return a IQueryable
datatype because in the query I'm joining two tables using fk relationship, am I wrong?
Thanks
Upvotes: 0
Views: 1377
Reputation: 181077
You're returning an "anonymous" datatype in the IQueryable, not a good habit since you can't access it outside of the method where it's created.
Try declaring a struct that can hold your data and return an IQueryable<CreditoStruct> instead, or even - since you're just wanting a single value - call Single() on the IQueryable to get a CreditoStruct and return that.
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();
}
Upvotes: 2
Reputation: 2317
if You are expecting just one result use the Single
Method.
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();
Also you should parse your result to a concrete type instead of an anounymus type
Lets say you create a class
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;}
}
and then You can Use it like this
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();
if you want to return various of them use instead of Single, toList() and thats it.
Then define your function to return a Credito
type (with Single()
) or List<Credito>
(with ToList()
)
Upvotes: 3