LINQ to SQL大查询元组
作者:互联网
我从一开始就没有正确地计划项目,所以我最终在页面周围散布了许多SQL查询.
因此,为了避免在所有其他方法中捕获Sql异常或由于一种方法中的异常而导致页面丢失一半,我想提取所有需要的数据,因此:
这是dll文件中的方法,我需要将其返回,以便在上下文处理后可以使用对象,但是我做错了什么(第一个查询finalproduct返回空):
public static Tuple<Product, IEnumerable<Tag>, IEnumerable<ProductComment>, IEnumerable<ProductVote>, IEnumerable<Review>, IEnumerable<ReviewComment>, IEnumerable<ReviewVote>> GetBigProduct(int productID)
{
using (ProductClassesDataContext context = new ProductClassesDataContext())
{
var outproduct = from product in context.Products
where product.ID == productID
join producttag in context.ProductTags on product.ID equals producttag.productID
join tag in context.Tags on producttag.TagID equals tag.ID
join productComment in context.ProductComments on product.ID equals productComment.productID
join productVote in context.ProductVotes on product.ID equals productVote.productID
join review in context.Reviews on product.ID equals review.productID
join reviewComment in context.ReviewComments on review.ID equals reviewComment.reviewID
join reviewVote in context.ReviewVotes on review.ID equals reviewVote.reviewID
select new Tuple<Product, Tag, ProductComment, ProductVote, Review, ReviewComment, ReviewVote>
(product, tag, productComment, productVote, review, reviewComment, reviewVote);
var finalProduct = (from t in outproduct select t.Item1).Single();
var finalTags = (from t in outproduct select t.Item2).ToList();
var finalProductComments = (from t in outproduct select t.Item3).ToList();
var finalProductVotes = (from t in outproduct select t.Item4).ToList();
var finalReviews = (from t in outproduct select t.Item5).ToList();
var finalReviewsComments = (from t in outproduct select t.Item6).ToList();
var finalReviewsVotes = (from t in outproduct select t.Item7).ToList();
return new Tuple<Product, IEnumerable<Tag>, IEnumerable<ProductComment>, IEnumerable<ProductVote>, IEnumerable<Review>, IEnumerable<ReviewComment>, IEnumerable<ReviewVote>>
(finalProduct, finalTags, finalProductComments, finalProductVotes, finalReviews, finalReviewsComments, finalReviewsVotes);
}
}
解决方法:
您确定在outproduct select t.Item1查询中t仅返回了一个元素吗?
根据documentation,Single():
Returns the only element of a sequence, and throws an exception if
there is not exactly one element in the sequence.
您可能需要First()或FirstOrDefault().
评论后更新
我认为问题在于您的加入.实际上,我认为您不想在这里加入.我想您需要的是以下内容:
var outproduct = from product in context.Products
where product.ID == productID
select new Tuple<Product, ProductComment, Tag>(
product, // product itself
context.ProductComments.Where(p => p.productID == product.ID).ToList(), // list of comments for this product
(from pt in context.ProductTags
join tag in context.Tags on pt.TagID equals tag.ID
where pt.productID = product.ID
select tag).ToList() // list of tags for this product
// ... more dimensions in tuple
);
然后,从成品查询中,您将得到一个元组集合,其中包含<产品,ProductComments列表,标签列表>.
您可以轻松添加其余的元组尺寸.为了简单起见,我删除了它们.
您现在可以直接从函数中返回此元组
return outproduct.FirstOfDefault()
不需要var finalProduct =样式的行并重建最终的元组.
标签:linq-to-sql,asp-net,c 来源: https://codeday.me/bug/20191201/2082093.html