2013-01-22 41 views
0

我想將DataTable和List之間的LINQ結合結果結合起來。將數據表和列表結合後使用DataTable和列表結合結果<Object>

這只是正常:

var lpYear = (
    from a in _ds.Tables[0].AsEnumerable() 
    join b in LandingPages on a["OFFERINGKEY"].ToString() equals b.Code into c 
    from d in c.DefaultIfEmpty() 
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("January 1, " + year) 
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("December 31, " + year) 
    where LandingPages.Any(x => x.Code == a["OFFERINGKEY"].ToString()) 
    orderby d.Title 
    select new { 
     title = d.Title, 
     price = a["PRICE"] 
    }).GroupBy(o => o.title) 
    .Select(o => new { 
     total = o.Sum(p => decimal.Parse(p.price.ToString())), 
     count = o.Count(), 
     title = o.Key 
    } 
); 

我結束了包含 「total | count | title」 行。

我想要做的是添加更多的列。例如,LandingPage.URLLandingPage.Code。我已經試過這樣的,但它不工作:

var lpYear = (
    from a in _ds.Tables[0].AsEnumerable() 
    join b in LandingPages on a["OFFERINGKEY"].ToString() equals b.Code into c 
    from d in c.DefaultIfEmpty() 
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("January 1, " + year) 
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("December 31, " + year) 
    where LandingPages.Any(x => x.Code == a["OFFERINGKEY"].ToString()) 
    orderby d.Title 
    select new { 
     title = d.Title, 
     price = a["PRICE"], 
     url = d.URL, 
     code = d.Code 
    }).GroupBy(o => o.title) 
    .Select(o => new { 
     total = o.Sum(p => decimal.Parse(p.price.ToString())), 
     count = o.Count(), 
     title = o.Key, 
     url = o.Select(p=>p.url), 
     code = o.Select(p=>p.code) 
    } 
); 

這是urlpurchased結果值:

System.Linq.Enumerable+WhereSelectEnumerableIterator`2[<>f__AnonymousType2`3[System.String,System.Object,System.String],System.String] 

解決方案(感謝Cédric Bignon

地方.First()在我的o.Select(p=>p.url)行末:

url = o.Select(p=>p.url).First(), 
code = o.Select(p=>p.code).First() 
+0

你爲什麼不使用LINQ語法(而不是擴展方法)從查詢的開始到結束?它可以簡化查詢的多個部分之間的引用。當你說「這行不通時,究竟是什麼問題?」 –

+0

我嘗試過使用各種擴展方法配置(我認爲),並且使用'.GroupBy()'和'.Select()'似乎是唯一能夠滿足需要的方法。你能提供一個例子嗎? – jiy

回答

1

剛纔忘了ToList(),當你做o.Select(...)

var lpYear = (
    from a in _ds.Tables[0].AsEnumerable() 
    join b in LandingPages on a["OFFERINGKEY"].ToString() equals b.Code into c 
    from d in c.DefaultIfEmpty() 
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("January 1, " + year) 
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("December 31, " + year) 
    where LandingPages.Any(x => x.Code == a["OFFERINGKEY"].ToString()) 
    orderby d.Title 
    select new { 
     title = d.Title, 
     url = d.URL, 
     price = a["PRICE"], 
     purchased = a["PURCHASEDATE"].ToString() 
    }).GroupBy(o => o.title) 
    .Select(g => new { 
     total = g.Sum(p => decimal.Parse(p.price.ToString())), 
     count = g.Count(), 
     title = g.Key, 
     url = g.Select(p=>p.url).Distinct().Single(), 
     code = g.Select(p=>p.code).Distinct().Single() 
    } 
); 

在純LINQ枚舉...枚舉:

var lpYear = from o in (from a in _ds.Tables[0].AsEnumerable() 
         join b in LandingPages on a["OFFERINGKEY"].ToString() equals b.Code into c 
         from d in c.DefaultIfEmpty() 
         where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("January 1, " + year) 
         where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("December 31, " + year) 
         where LandingPages.Any(x => x.Code == a["OFFERINGKEY"].ToString()) 
         orderby d.Title 
         select new 
         { 
          title = d.Title, 
          url = d.URL, 
          price = a["PRICE"], 
          purchased = a["PURCHASEDATE"].ToString() 
         }) 
      group o by o.title into g 
      select new 
      { 
       total = g.Sum(p => decimal.Parse(p.price.ToString())), 
       count = g.Count(), 
       title = g.Key, 
       url = (from p in g 
         select p.url).Distinct().Single(), 
       code = (from p in g 
         select p.code).Distinct().Single() 
      }; 
+0

添加'.ToList()'後,它會爲'url'和'purchased'返回以下內容:'System.Collections.Generic.List'1 [System.String]' – jiy

+0

是的,這是正常行爲,因爲您在做一個分組。 _g_可以包含多個具有不同_price_或_purchased_值的元素。如果您確定組_g_的所有元素的值都相同,請使用_Single_而不是_ToList_。 –

+0

好吧,明白了。 '.Single()'產生了一個錯誤,但'.First()'像一個魅力一樣工作。你的「純LINQ」例子也可以工作,並且在14行上做同樣的事情(它缺少結尾')')。謝謝你的幫助! – jiy