I used LINQ to query two tables and showed the result in a var Spelers. This works fine but now I want to re-use this query outside the method after first clearing the var. I searched all resources known to me but couldn't find a solution.
The query Spelers uses the ObserverableCollections speler and golfclub. I try to create an app for my C# exam and I am c# novice. I hope someone can help me with this because I want all queries to be public accessible.
public void JoinData()
{
var Spelers = (from spel in speler
join club in golfclub
on spel.ClubId equals club.Id
select new
{
Id = spel.Id,
Voornaam = spel.Voornaam,
Achternaam = spel.Achternaam,
Handicap = spel.Handicap,
Telefoon = spel.Telefoon,
Email = spel.Email,
Homeclub = club.Naam,
ClubId = spel.ClubId,
TeamId = spel.TeamId,
PuntenVoor = spel.PuntenVoor,
PuntenTegen = spel.PuntenTegen
}).ToList();
if (Datagrid.ItemsSource == null)
{
Datagrid.ItemsSource = Spelers;
}
}
CodePudding user response:
I would strongly suggest separate type for such big set of various data.
For example:
public class MyDataCarrier
{
public int Id { get; set; }
public string Voornaam { get; set; }
public string Achternaam { get; set; }
public int Handicap { get; set; }
public string Telefoon { get; set; }
public string Email { get; set; }
public string Homeclub { get; set; }
public int ClubId { get; set; }
public int TeamId { get; set; }
public int PuntenVoor { get; set; }
public int PuntenTege { get; set; }
}
And then just write the method:
public List<MyDataCarrier> GetData()
{
return (
from spel in speler
join club in golfclub
on spel.ClubId equals club.Id
select new MyDataCarrier
{
// here you assing each property
})
.ToList();
}
And then you can easily reuse it wherever you want:
if (DataGrid.ItemsSource is null)
{
DataGrid.DataSource = GetData();
}
CodePudding user response:
First of all, we should extract Speler
as a property; we have to change anonymous type for something more suitable, let
it be a named tuple (custom class is another possibility):
//TODO: Please, check types! Is "int Handicap" a correct guess?
public List<(
int Id,
string Voornaam,
string Achternaam,
int Handicap,
string Telefoon,
string Email,
string Homeclub,
int ClubId,
int TeamId,
int PuntenVoor,
int PuntenTegen
)> Speler { get; private set; } = new ();
Then you can put
public void JoinData() {
Spelers = (from spel in speler
join club in golfclub
on spel.ClubId equals club.Id
select (spel.Id,
spel.Voornaam,
spel.Achternaam,
spel.Handicap,
spel.Telefoon,
spel.Email,
club.Naam,
spel.ClubId,
spel.TeamId,
spel.PuntenVoor,
spel.PuntenTegen))
.ToList();
if (Datagrid.ItemsSource == null)
Datagrid.ItemsSource = Spelers;
}
CodePudding user response:
If you don't want your method to have a return value, you can have the value outside the method as well.
Var Spelers=new List<object>();
public void JoinData()
{
Spelers = (from spel in speler
join club in golfclub
on spel.ClubId equals club.Id
select new
{
Id = spel.Id,
Voornaam = spel.Voornaam,
Achternaam = spel.Achternaam,
Handicap = spel.Handicap,
Telefoon = spel.Telefoon,
Email = spel.Email,
Homeclub = club.Naam,
ClubId = spel.ClubId,
TeamId = spel.TeamId,
PuntenVoor = spel.PuntenVoor,
PuntenTegen = spel.PuntenTegen
}).ToList();
if (Datagrid.ItemsSource == null)
{
Datagrid.ItemsSource = Spelers;
}
}