使用我的Repository类,我使用LinqToSql
从存储库中检索数据(例如,在我的示例中为Sql Server 2008)。我将结果数据放入POCO
对象。效果很好:))
现在,如果我的POCO
对象具有子属性((另一个POCO
对象或IList),我正试图找出一种填充该数据的方法。我不太确定如何做到这一点。
这是我的一些示例代码。请注意我设置的最后一个属性。它编译,但它不是'正确'。它不是POCO对象实例..而且我不确定如何编写最后一行。
public IQueryable<GameFile> GetGameFiles(bool includeUserIdAccess)
{
return (from q in Database.Files
select new Core.GameFile
{
CheckedOn = q.CheckedOn.Value,
FileName = q.FileName,
GameFileId = q.FileId,
GameType = (Core.GameType)q.GameTypeId,
IsActive = q.IsActive,
LastFilePosition = q.LastFilePosition.Value,
UniqueName = q.UniqueName,
UpdatedOn = q.UpdatedOn.Value,
// Now any children....
// NOTE: I wish to create a POCO object
// that has an int UserId _and_ a string Name.
UserAccess = includeUserIdAccess ?
q.FileUserAccesses.Select(x => x.UserId).ToList() : null
});
}
注意:
我现在有一个建议将孩子的结果提取到他们各自的POCO
课程中,但这就是Visual Studio Debugger
所说的课程: -
为什么是System.Data.Linq.SqlClient.Implementation.ObjectMaterializer<..>
.Convert<Core.GameFile>
而非List<Core.GameFile>
包含POCO's
?
有什么建议是什么/我做错了什么?
这就是我将儿童数据提取到各自的poco中所做的工作..
// Now any children....
UserIdAccess = includeUserIdAccess ?
(from x in q.FileUserAccesses
select x.UserId).ToList() : null,
LogEntries = includeUserIdAccess ?
(from x in q.LogEntries
select new Core.LogEntry
{
ClientGuid = x.ClientGuid,
ClientIpAndPort = x.ClientIpAndPort,
// ... snip other properties
Violation = x.Violation
}).ToList() : null
答案 0 :(得分:1)
我认为您需要做的就是在此处添加另一个Linq查询:
q.FileUserAccesses.Select(x => x.UserId).ToList()
即。你想从FileUserAccess记录中选择数据 - 我假设它是Linq to SQL类,所以要做到这一点你可以有类似的东西:
(from fua in q.FileUserAccesses
select new PocoType
{
UserID = fua.UserID,
Name = fua.User.UserName // Not sure at this point where the name comes from
}).ToList()
这应该让你至少指向正确的方向。
答案 1 :(得分:0)
UserIdAccess的类型是什么?怎么不'对'?你收到'错误的'数据了吗?如果是这样,你直接检查了你的数据库,以确保“正确”的数据存在吗?