0

以下のクエリは、「住所」テーブルに結合されたリードのリストを返します。リードごとに複数存在する可能性があります。

モデル内の都市ビューで住所が連結され、コンマで区切られた、リードごとに 1 つの応答を返したいと考えています。結合結果に都市がない場合は、'-' が返されます。

電流出力

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

望ましい出力

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

クエリ

 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

文字列結合を試してください:

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

あなたの例:

 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 に答える