Pur*_*ome 11 .net c# sql linq linq-to-entities
我有两个linq(到EF4)查询,返回不同的结果.第一个查询包含正确的结果,但未正确格式化/投影.
第二个查询是我想要的,但它缺少一些数据.
alt text http://img220.imageshack.us/img220/9678/schema.png
var xxxx = (from cp in _connectedClientRepository
.GetConnectedClients(new[] { "LogEntry", "LogEntry.GameFile" })
.AsExpandable()
.Where(predicate)
select cp)
.ToList();
Run Code Online (Sandbox Code Playgroud)
alt text http://img231.imageshack.us/img231/6541/image2ys.png
注意该财产GameFile.它不是空的.这很棒:)请注意linq查询?我渴望加载一个LogEntry然后急切加载GameFile(对于每个渴望加载的LogEntry).
这就是我所追求的 - >对于每一个LogEntry渴望加载的人,请急切加载GameFile.但这个预测结果是错误的......
好的......接下来......
var yyy = (from cp in _connectedClientRepository
.GetConnectedClients(new[] { "LogEntry", "LogEntry.GameFile" })
.AsExpandable()
.Where(predicate)
select cp.LogEntry)
.ToList();
Run Code Online (Sandbox Code Playgroud)
alt text http://img24.imageshack.us/img24/4417/image1tu.png
注意:上面的图片中有一个拼写错误...请注意包含关联类型代码是正确的(即.LogEntry.GameFile),而图像上的拼写错误.
现在正确投影 - >所有LogEntries结果.但请注意该GameFile属性现在如何为空?我不确定为什么:(我以为我正确地渴望加载正确的链.所以这是正确的投影,但结果不正确.
public IQueryable<ConnectedClient> GetConnectedClients(
string[] includeAssociations)
{
return Context.ConnectedClients
.IncludeAssociations(includeAssociations)
.AsQueryable();
}
public static class Extensions
{
public static IQueryable<T> IncludeAssociation<T>(
this IQueryable<T> source, string includeAssociation)
{
if (!string.IsNullOrEmpty(includeAssociation))
{
var objectQuery = source as ObjectQuery<T>;
if (objectQuery != null)
{
return objectQuery.Include(includeAssociation);
}
}
return source;
}
public static IQueryable<T> IncludeAssociations<T>(
this IQueryable<T> source, params string[] includeAssociations)
{
if (includeAssociations != null)
{
foreach (string association in includeAssociations)
{
source = source.IncludeAssociation(association);
}
}
return source;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1669 次 |
| 最近记录: |