Question

In my project one table is connected with two or more tables ,To require wanted out put need to join them,Join is not my problem ,after join want to select desired column but from a segregate expression , as like bellow syntax:

public IEnumerable GetParent(string organogramType = "")
{
    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(SelectSearchColumns).ToList();
    return query;   
}
public Expression<Func<CmnCompany, CmnCompanyReference, CmnPartnerDetail, dynamic>> SelectSearchColumns = (p, r,t) => new

{
    CompanyID = p.CompanyID,
    CompanyName=p.CompanyName,
    PartnerName=t.PartnerName,
    OrganogramType=p.OrganogramType,
    ParentID=p.ParentID,
    InceptionDate=p.InceptionDate,
    RefName=r.RefName,
};
  1. Want to segregate select statement from my bellow linq syntax

  2. base on method parameter=organogramType select column name will be change suppose organogramType (Company, office, departmentName) office then select columnn “CompanyName “ will be chage with Office,how to chage column name on run time

Show me ERROR MESSAGE: A query body must end with a select clause or a group clause

If have any query please ,thanks in advanced

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top