16

我有这个方法:

public CampaignCreative GetCampaignCreativeById(int id)
        {
            using (var db = GetContext())
            {
                return db.CampaignCreatives
                    .Include("Placement")
                    .Include("CreativeType")                    
                    .Include("Campaign")
                    .Include("Campaign.Handshake")
                    .Include("Campaign.Handshake.Agency")
                    .Include("Campaign.Product")
                    .AsNoTracking()
                    .Where(x => x.Id.Equals(id)).FirstOrDefault();
            }
        }

我想使包含列表动态化。我试过:

public CampaignCreative GetCampaignCreativeById(int id, string[] includes)
        {
            using (var db = GetContext())
            {
                var query = db.CampaignCreatives;

                foreach (string include in includes)
                {
                    query = query.Include(include);
                }

                return query.AsNoTracking()
                    .Where(x => x.Id.Equals(id)).FirstOrDefault();                    
            }
        }

但它没有编译。我收到了这个错误:

无法将类型“System.Data.Entity.Infrastructure.DbQuery”隐式转换为“System.Data.Entity.DbSet”。存在显式转换(您是否缺少演员表?)

有谁知道如何使包含列表动态化?

谢谢

4

4 回答 4

37

我更喜欢定义包含的非字符串表达方式。主要是因为它不依赖于魔术字符串。

对于示例代码,它看起来像这样:

public CampaignCreative GetCampaignCreativeById(int id) {
    using (var db = GetContext()) {
        return db.CampaignCreatives
            .Include(cc => cc.Placement)
            .Include(cc => cc.CreativeType)                    
            .Include(cc => cc.Campaign.Select(c => 
                 c.Handshake.Select(h => h.Agency)))
            .Include(cc => cc.Campaign.Select(c => c.Product)
            .AsNoTracking()
            .Where(x => x.Id.Equals(id))
            .FirstOrDefault();
    }
}

为了使这些动态化,您可以这样做:

public CampaignCreative GetCampaignCreativeById(
    int id, 
    params Expression<Func<T, object>>[] includes
) {
    using (var db = GetContext()) {
        var query = db.CampaignCreatives;
        return includes
            .Aggregate(
                query.AsQueryable(), 
                (current, include) => current.Include(include)
            )
            .FirstOrDefault(e => e.Id == id);
    }
}

这是这样使用的:

var c = dataService.GetCampaignCreativeById(
     1, 
     cc => cc.Placement, 
     cc => cc.CreativeType, 
     cc => cc.Campaign.Select(c => c.Handshake.Select(h => h.Agency)),
     cc => cc.Campaign.Select(c => c.Product
);
于 2013-10-04T11:09:16.917 回答
15

使query变量可查询:

public CampaignCreative GetCampaignCreativeById(int id, string[] includes)
{
    using (var db = GetContext())
    {
        var query = db.CampaignCreatives.AsQueryable();
        foreach (string include in includes)
        {
            query = query.Include(include);
        }

        return query
            .AsNoTracking()
            .Where(x => x.Id.Equals(id))
            .FirstOrDefault();                    
    }
}
于 2012-04-03T20:46:49.010 回答
2

IQueryable<CampaignCreative>通过使用而不是给编译器一个提示var也可以。

IQueryable<CampaignCreative> query = db.CampaignCreatives;
// or
DbQuery<CampaignCreative> query = db.CampaignCreatives;

使用编译器var推断which 比返回的类型更具体(即(=base class of ) implementation ),因此您不能再将结果分配给变量。因此编译器错误就行了。DbSet<T>queryIncludeDbQuery<T>DbSet<T>IQueryable<T>queryquery = query.Include(include)

于 2012-04-03T22:27:25.887 回答
1

我编写了这个方法来根据它们的类型动态检索任何一组实体。我使用该IDbEntity接口提供了一个有效的密钥来搜索所有类中的 userId。该Util.GetInverseProperties<T>()方法用于获取Include语句中所需的属性。

public IEnumerable<T> GetItems<T>(string userId) where T : class, IDbEntity
{
    var query = db.Set<T>().Where(l => l.UserId==userId);

    var props = Util.GetInverseProperties<T>();
    foreach (var include in props)
        query = query.Include(include.Name);

    return query
        .AsNoTracking()
        .ToList();
}
public interface IDbEntity
{
    public string UserId { get; set; }
}
public static List<PropertyInfo> GetInverseProperties<T>()
{
    return typeof(T)
        .GetProperties()
        .Where(p => Attribute.IsDefined(p, typeof(InversePropertyAttribute)))
        .ToList();
}
于 2020-02-14T21:31:51.207 回答