我有一个使用1个表+大量视图的LINQ查询。我希望能够写出这样的东西:
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 };
上面的C#是用来保护无辜者(我)的伪代码。问题的要点是:我有一个与视图结果相关的查询。然而,这种观点可能是许多不同观点之一。所有视图都返回相同的数据类型。我认为我可能能够创建一个IQueryable,它包含我需要的ID并使用该查询。唉,这种努力已经停滞不前。
答案 0 :(得分:1)
你的问题是mostViewedWriters
没有命名字段,因为它只是一个返回Guid
列表的集合/查询,所以你应该像这样重写你的查询:
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 };
第二个连接条件直接连接到行的值,因为它只是一个Guid。 (我也改变了你的变量名,试图让它更清晰一点)