0

The query below returns a list of leads joined to an "address" table, of where there can be multiple per lead.

I'm looking to return one response per lead, with addresses concatenated in the city view within the model and seperated by commas. If there are no cities in the join result, it should return '-'.

CURRENT OUTPUT

Company Name | Company City
===========================
Company 1    | Glasgow
Company 1    | London
Company 2    | London
Company 3    | NULL

DESIRED OUTPUT

===========================
Company 1    | Glasgow, London
Company 2    | London
Company 3    | -

QUERY

 return (from t1 in db.Opportunities
                    from s1 in db.OpportunityStatus.Where(x => x.OpportunityStatus_ID == t1.StatusReason_ID)
                    from t2 in db.Leads.Where(x => x.Lead_ID == t1.Lead_ID)
                    from t3 in db.LeadAddresses.Where(x => x.Lead_ID == t2.Lead_ID).DefaultIfEmpty()
                    from t4 in db.Addresses.Where(x => x.Address_ID == t3.Address_ID).DefaultIfEmpty()
                    orderby (t1.Created) descending
                    select new FieldSalesPipelineViewModel
                    {
                        Id = t1.Opportunity_ID,
                        CompanyName = t2.Company_Name,
                        OpportunityTitle = t1.Opportunity_Title,
                        CompanyCity = "",
                        OpportunityStatusName = s1.OpportunityStatus_Name
                    }).Take(howMany);
4

1 回答 1

1

Try a string-join:

CompanyCity = string.Join(",", (from p in db.Opportunities where t2.companyname == p.companyname select p.Companycity)

For your example:

 return (from t1 in db.Opportunities
                    from s1 in db.OpportunityStatus.Where(x => x.OpportunityStatus_ID == t1.StatusReason_ID)
                    from t2 in db.Leads.Where(x => x.Lead_ID == t1.Lead_ID)
                    from t3 in db.LeadAddresses.Where(x => x.Lead_ID == t2.Lead_ID).DefaultIfEmpty()

                    orderby (t1.Created) descending
                    select new FieldSalesPipelineViewModel
                    {
                        Id = t1.Opportunity_ID,
                        CompanyName = t2.Company_Name,
                        OpportunityTitle = t1.Opportunity_Title,
                        CompanyCity = string.Join(",", (from t4 in db.Addresses Where t4.Address_ID == t3.Address_ID select t4.CompanyCity),
                        OpportunityStatusName = s1.OpportunityStatus_Name
                    }).Take(howMany);
于 2012-05-04T08:49:05.740 回答