Reputation: 6796
I am not good with C# lists, because it is very confusing to me. So now I joined two tables into each other which looks like this:
var final = queryTable
.Join(skillTable,
query => query.Id_Skill,
skill => skill.Id,
(query, skill) => new { Id = query.Id, Name = query.Name, Line1 = skill.Line1, Line2 = skill.Line2, Line3 = skill.Line3, Line4 = skill.Line4, Line5 = skill.Line5 })
.ToList();
So as you can see the new list hast the elements Id
, Name
, Line1
, Line2
, Line3
, Line4
and Line5
.
What is the type of this list? What I mean is, how would I declare the list instead of var? I need this for further purpose.
Like:
List<theType> final = new List<theType>();
final = ...
Upvotes: 1
Views: 120
Reputation: 823
if you need to reference this later you could create a class with all those properties like this
public class SomeName
{
int id { get; set; }
string Name { get; set; }
string Line1 { get; set; }
string Line2 { get; set; }
string Line3 { get; set; }
string Line4 { get; set; }
}
and change your query to create a new instance of this class
var final = queryTable
.Join(skillTable,
query => query.Id_Skill,
skill => skill.Id,
(query, skill) => new SomeName() { Id = query.Id, Name = query.Name, Line1 = skill.Line1, Line2 = skill.Line2, Line3 = skill.Line3, Line4 = skill.Line4, Line5 = skill.Line5 })
.ToList();
Upvotes: 3
Reputation: 66489
From the documentation on anonymous types: (emphasis added)
Typically, when you use an anonymous type to initialize a variable, you declare the variable as an implicitly typed local variable by using var. The type name cannot be specified in the variable declaration because only the compiler has access to the underlying name of the anonymous type. For more information about var, see Implicitly Typed Local Variables.
If you'd like to reuse the type after the query has been executed, the best option is probably creating a class for just that purpose.
An alternative solution is to use Tuple
, which saves you creating another class but is a pain to use if you plan on referencing the type in too many locations. I'd probably use the Tuple
only if you plan on further manipulating the data within the same method.
(query, skill) => Tuple.Create(query.Id, query.Name, skill.Line1, skill.Line2,
skill.Line3, skill.Line4, skill.Line5 });
You'll end up with a List<Tuple<int, string, .... >
Upvotes: 2
Reputation: 7547
You're creating an anonymous type using the new { }
syntax. This means you can only use var
, unless you specifically create a class with the same fields.
Upvotes: 0