简体   繁体   中英

How can I combine a LINQ query with an IQueryable<Guid>

I have a LINQ query that uses 1 table + a large number of views. I'd like to be able to write something like this:

IQueryable<Guid> mostViewedWriters;

switch (datePicker)
{
    case DatePicker.Last12Hours:
        mostViewedWriters = from x in context.tempMostViewed12Hours
                            select x.GuidId;
        break;
    case DatePicker.Last24Hours:
        mostViewedWriters = from x in context.tempMostViewed24Hours
                            select x.GuidId;
        break;
    case DatePicker.Last36Hours:
        mostViewedWriters = from x in context.tempMostViewed36Hours
                            select x.GuidId;
        break;
}

var query = from x1 in context.Articles
join x2 in context.Authors on x1.AuthorId == x2.AuthorId
join x3 in mostViewedWriters on x2.AuthorId == x3.Id
select new { x2.AuthorName, x1.ArticleId, x1.ArticleTitle };

The above C# is pseudo-code written to protect the innocent (me). The gist of the question is this: I have a query that is related to the results of a view. That view, however, could be one of many different views. All the views return the same data type. I thought that I might be able to create an IQueryable that would contain the Ids that I need and use that query. Alas, that effort has stalled.

your problem is that mostViewedWriters doesn't have named fields because its just a collection/query that returns a list of Guid 's, so you should rewrite your query like this:

var query = from article in context.Articles
            join author in context.Authors on article.AuthorId == author.AuthorId
            join id in mostViewedWriters on author.AuthorId == id
            select new { x2.AuthorName, x1.ArticleId, x1.ArticleTitle };

The second join condition joins directly on the value from the row, because it is just a Guid. (I also changed your variable names to try to make it a little clearer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM