0
4

1 回答 1

1

You can't do that, because your expression needs three arguments, but from query you always receive single argument (which is sequence type). Also your query needs select statement after last join to eliminate error you see now. So, technically you can create type, which holds all three objects:

public class Foo
{
    public CmnCompany Company { get; set; }
    public CmnCompanyReference CompanyReference { get; set; }
    public CmnPartnerDetail PartnerDetail { get; set; }
}

And select that object from query (don't worry, query is not executed yet):

var query = 
   from p in this.Context.CmnCompanies
   where organogramType != "" && 
         !p.OrganogramType.Contains(organogramType) && 
         p.OrganogramType != null
   join r in this.Context.CmnCompanyCategories 
         on p.CompanyCategoryID equals r.CompanyCategoryID
   join s in this.Context.CmnCompanyReferences on p.RefID equals s.RefID
   join t in this.Context.CmnPartnerDetails on p.PartnerID equals t.PartnerID)
   select new Foo { 
       Company = p, 
       CompanyReference = s, 
       PartnerDetails = t 
   };

Then modify your expression (if property name matches original property name, you can skip property name specifying):

public Expression<Func<Foo, dynamic>> SelectSearchColumns = (f) => new    
{
    f.Company.CompanyID,
    f.Company.CompanyName,
    f.PartnerDetail.PartnerName,
    f.Company.OrganogramType,
    f.Company.ParentID,
    f.Company.InceptionDate,
    f.CompanyReference.RefName,
};

And do projection (query executed only when converted to list):

return query.Select(SelectSearchColumns).ToList();

If you will run SQL Profiler, you will see, that only columns selected in your expression are returned from database.

于 2013-02-17T08:25:27.983 回答