0

i have a code like this.

Context:

public class TestContext : System.Data.Entity.DbContext
{
    public TestContext()
        : base("name=TestContext")
    {
    }

    public System.Data.Entity.DbSet<TestClass> TestClass { get; set; }
    public System.Data.Entity.DbSet<TestSubClass> TestSubClass { get; set; }
}

TestClass:

public class TestClass
{
    [Key]
    public Guid Id { get; set; }
    public string Name { get; set; }
}

TestSubClass:

public class TestSubClass
{
    [Key]
    public Guid Id { get; set; }
    public int Status { get; set; }

    [Column("Test")]
    public Guid? TestId { get; set; }
    [ForeignKey("TestId")]
    public virtual TestClass Test { get; set; }
}

GetEverything: / Problem

public static IQueryable<TestClass> GetEverything(bool onlyVisible = false)
{
    IQueryable<TestClass> query;

    using(var context = new TestContext())
    {
        query = from test in context.TestClass
                select test;

        if (onlyVisible)
        {
            query
                .Join(context.TestSubClass, test => test.Id, testSub => testSub.TestId, (test, testSub) => new { TestClass = test, TestSubClass = testSub })
                .Where(test => test.TestSubClass.Status == 1)
            ;

            /*
             * I want to add something like this:
             * 
               query += 
                   join testSub in context.TestSubClass on test.Id equals testSub.TestId
                   where testSub.Status == 1
               ;
             */
        }
    }

    return query;
}

And if you look at the comment you know what i want.

The problem is that i dont know how to combine multiple of these things performance.

Or how I can combine that better?

I have a basic query string which would be extended by additional things. But i think these additional things are not pretty readable and understable for third persons...

3
  • TestClass - Moment, i edit something. Commented Dec 10, 2014 at 13:05
  • you can add where conditions to existing IQueryable before enumerating it. IQueryable.Include is the method for getting related entities in the same query. Commented Dec 10, 2014 at 13:08
  • Why i can't use something like this? query = query.Where(test => test.TestSubClass.Any(t => t.TestId == test.Id && t.Status == 1)); If i add public virtual ICollection<TestSubClass> TestSubClass { get; set; } to TestClass Commented Dec 10, 2014 at 13:12

1 Answer 1

1

I think you can try using the following:

public class TestClass
{
    [Key]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public ICollection<TestSubClass> TestSubs {get;set;}
}

public class TestSubClass
{
    [Key]
    public Guid Id { get; set; }
    public int Status { get; set; }

    [Column("Test")]
    public Guid? TestId { get; set; }
    [ForeignKey("TestId")]
    public virtual TestClass Test { get; set; }
}

public static IQueryable<TestClass> GetEverything(bool onlyVisible = false)
{
      var context = new TestContext();
      var query = from test in context.TestClass
                select test;

      if (onlyVisible)
      {
           query = query.Include(i => i.TestSubs).Where(w => w.TestSubs.Any(a => a.Status == 1));
      }
      return query;
}

Include instructs EF to eagerly load entities. Check the article on EF strategies for loading related entities

Sign up to request clarification or add additional context in comments.

17 Comments

Thats what i do at the moment :D But how to use the join in the query "string" and the where as extension?
@PatrickB I've updated my answer, not sure if it will work, though :D
Thats the same problem :D Maybe is my question not understable? :(
Can you define the query string and what will get in it?
You need to add using System.Data.Entity;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.