2011-03-14 19 views
5

Linq Birleşimine katıl seçeneğinde birden çok tabloya yayılan yüklemeleri dinamik olarak oluşturmak istiyorum.PredicateBuilder, birden çok tabloya yayılan yüklemler oluşturabilir mi?

değiştirin:

public class Foo 
{ 
    public int FooId; // PK 
    public string Name; 
} 

public class Bar 
{ 
    public int BarId; // PK 
    public string Description; 
    public int FooId; // FK to Foo.PK 
} 

void Test() 
{ 
    IQueryable<Foo> fooQuery = null; // Stubbed out 
    IQueryable<Bar> barQuery = null; // Stubbed out 

    IQueryable<Foo> query = 
     from foo in fooQuery 
     join bar in barQuery on foo.FooId equals bar.FooId 
     where ((bar.Description == "barstring") || (foo.Name == "fooname")) 
     select foo; 
} 

gibi bir şey ile:

void Test(bool searchName, bool searchDescription) 
{ 
    IQueryable<Foo> fooQuery = null; // Stubbed out 
    IQueryable<Bar> barQuery = null; // Stubbed out 

    IQueryable<Foo> query = 
     from foo in fooQuery 
     join bar in barQuery on foo.FooId equals bar.FooId 
     select foo; 

    // OR THIS 

    var query = 
     from foo in fooQuery 
     join bar in barQuery on foo.FooId equals bar.FooId 
     select new {foo, bar}; 

    var predicate = PredicateBuilder.False<Foo>(); 
    if (searchName) 
    { 
     predicate = predicate.Or(foo => foo.Name == "fooname"); 
    } 
    if (searchDescription) 
    { 
     // Cannot compile 
     predicate = predicate.Or(bar => bar.Description == "barstring"); 
    } 
    // Cannot compile 
    query = query.Where(predicate); 
} 
aşağıdaki kod parçacığını, ben şu kodda 'nerede' deyimi yerine PredicateBuilder veya benzer yapısını kullanmak istiyorum

Bu sorunun üstesinden gelmek için herhangi bir düşünce, fikir, strateji? senin yüklem yarım Foo hareket eden, diğer yarısı bir Bar açıksa -

sayesinde

EulerOperator

cevap

6

Ben senin sorunun PredicateBuilder tipi T ile olduğunu düşünüyorum.

Basit el inşa sorgusu ile bu yerini alabilir:

void Test() 
{ 
    IQueryable<Foo> fooQuery = null; // Stubbed out 
    IQueryable<Bar> barQuery = null; // Stubbed out 

    IQueryable<Foo> query = 
     from foo in fooQuery 
     join bar in barQuery on foo.FooId equals bar.FooId 
     select new {Foo = foo, Bar = bar}; 

    if (searchName) 
    { 
     query = query.Where(fb => fb.Foo.Name == "fooname"); 
    } 
    if (searchDescription) 
    { 
     query = query.Where(fb => fb.Bar.Description == "barstring"); 
    } 

    // use query here 
} 

alternatif bir yöntem Foo, Bar çift çalışması için ancak PredicateBuilder kullanmaktır - mesela

class FooBar 
{ 
    public Foo Foo {get;set;} 
    public Bar Bar {get;set;} 
} 

void Test(bool searchName, bool searchDescription) 
{ 
    IQueryable<Foo> fooQuery = null; // Stubbed out 
    IQueryable<Bar> barQuery = null; // Stubbed out 

    var query = 
     from foo in fooQuery 
     join bar in barQuery on foo.FooId equals bar.FooId 
     select new FooBar 
     { 
      Foo = foo, 
      Bar = bar 
     }; 

    var predicate = PredicateBuilder.False<FooBar>(); 
    if (searchName) 
    { 
     predicate = predicate.Or(foobar => foobar.Foo.Name == "fooname"); 
    } 
    if (searchDescription) 
    { 
     predicate = predicate.Or(foobar => foobar.Bar.Description == "barstring"); 
    } 
    query = query.Where(predicate); 

    // use query here 
} 
İlgili konular