0

I'd like to combine the results of a LINQ join between a DataTable and a List.

This works just fine:

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
    }
);

And I end up with rows containing "total | count | title".

What I'd like to do is add some more columns. For example, LandingPage.URL or LandingPage.Code. I've tried like this, but it doesn't work:

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)
    }
);

This is the resulting value for url and purchased:

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

The Solution (thanks to Cédric Bignon):

Place .First() at the end of my o.Select(p=>p.url) lines:

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

Unfortunately there isn't one or two APIs/Libraries/Frameworks you can knit together to produce a video serving website.

Invariably this will require heavy involvement on all levels of the stack:

Server back-end will require the following problems to be solved:

  • Video Encoding
    • FFMPEG or MPlayer experience for encoding any number of video formats to either FLV or more recent h264 for HTML5 supported formats
    • A reliable mechanism to transcode video in a background process; initially on one server but eventually on multiple servers as your services scales
    • Video resizing
  • Bandwidth Management to throttle connection just enough so that the video trickles down to the user
  • Storing video files and a file sharding and naming mechanism
  • API Server - Something like Rails, Django or NodeJS Express to serve as a JSON service layer between web clients and the video encoding/serving service.

Front end will require the following issues to be solved:

  • Playing back the video reliably across multiple OSes (Windows, OSX, Linux, Tablets, Mobile) and Platforms (IE, Chrome/Safari, Firefox, Opera) with fallback support for older browsers
  • DRM - are your videos free or commercial? If the latter, this is another issue that needs to be addressed

I'd strongly recommend an Event Driven system on your back-end as it is much easier to develop code that supports concurrency. NodeJS would be a good pick. It is worth looking at node-fluent-ffmpeg module for NodeJS as a good starting point.

As for your front-end I'd recommend frameworks such as Backbone.js or AngularJS to develop you web-app.

It was a fun and challenging journey when I attempted something similar a few years ago. I wish you good fortune in your journey.

4

1 回答 1

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()
             };
于 2013-01-22T15:06:21.740 回答