开发者

Creating a list of .Include() in linq to entities

开发者 https://www.devze.com 2023-01-15 10:00 出处:网络
I have a long list of includes: .Include(\"x\") .Include(\"y\") .Include(\"z\") .Include(\"z.w\") .Include(\"z.v\")

I have a long list of includes:

.Include("x")
.Include("y")
.Include("z")
.Include("z.w")
.Include("z.v")

I would like to use this list on three different queries. How can I put these in开发者_如何学运维 a list and use that list in all my queries in order to not repeat myself.


Try:

public static class MyQueryHelpers
{
    public static ObjectQuery<Foo> MyIncludes(this ObjectQuery<Foo> query)
    {
        return query.Include("x")
                    .Include("y")
                    .Include("z")
                    .Include("z.w")
                    .Include("z.v");
    }
}

Now use it:

var q = from f in Context.Foos.MyIncludes()
        select f;
0

精彩评论

暂无评论...
验证码 换一张
取 消