LINQ2OBJECTS: Help with query. Creating a list on the fly
I have a class created from XML Schema. And i have the following query, the query开发者_C百科 returns well as an anonymous method
var test1 = from t in test.Test
join p in test.Params on t.Test_Id equals p.Test_Id
join p1 in test.Param on p.Params_Id equals p1.Params_Id
select new { Name = t.Name, Params = p1};
It returns 4 records, because i have 2 Tests and each tests has 2 params.
I am using an anonymous type for testing. I notice in the above example Params is actually of type ParamRow.
I would like to put it into my own class that looks like this.
public class MyOwnClass
{
public string Name { get; set; }
public IList<Param> Params { get; set; }
}
my Param is like this, which is included as an ILIST
public class Param
{
public string Name { get; set; }
public bool Enabled { get; set; }
}
I am having an issue to create the new "PARAM" in my new class and assign Name and Enabled..
Name should be assigned to p1.Name
and
Enabled should be assigned to p1.Enabled
So what i need eventually is 2 records returns and each record as 2 params as an ILIST with it.
Confused :-)
Can anyone help?
Hence i should end up with
Test1
Params
Param1
Param2
Test2
Params
Param1
Param2
I hope that makes sense?
This gives you what you want
var test1 = from t in test.Test
select new MyOwnClass {
Name = t.Name,
Params = (from p in test.Params
join p1 in test.Param on p.Params_Id equals p1.ParamsId
where p.Test_Id == t.Test_Id
select new Param {
Name = p1.Name,
Enabled = p1.Enabled
}).ToList()
};
精彩评论