简体   繁体   English

序列没有匹配的元素(LET CLAUSE)

[英]Sequence has no matching elements (LET CLAUSE)

Im no linq shark, but I have a problem I cant seem to solve. 我不是linq鲨鱼,但我有一个我似乎无法解决的问题。 Here is my code: 这是我的代码:

        using (var dwCtx = new NordicDWEntities())
        using (var ctx = new NordicDSA())
        {
            var rawClusters = ctx.SurveyClusters.Include(x => x.SurveyClusterVariables).Where(x => x.SurveyId == request.ProjectId && x.SurveyCode == request.SurveyCode).ToArray();
            var masterIds = rawClusters.SelectMany(x => x.SurveyClusterVariables.Select(y => y.MasterVariableID)).Distinct().ToArray();
            var masterVariables = dwCtx.SurveyMasterVariables.Where(x => masterIds.Contains(x.MasterVariableID)).ToArray();

            var values =
                (from r in ctx.STAGE_ResponseOption
                 join m in ctx.SurveyVariableMaps on r.VariableCode equals m.VariableCode
                 where
                     m.SurveyId == request.ProjectId
                     && m.SurveyCode == request.SurveyCode
                     && (m.IgnoreVariable.HasValue && m.IgnoreVariable < 1)
                     && masterIds.Contains(m.MasterVariableID.Value)
                     && r.SurveyCode == request.SurveyCode
                 select new { Value = r, Map = m }).ToArray();

            var clusters =
                (from c in rawClusters
                 where c.SurveyId == request.ProjectId && c.SurveyCode == request.SurveyCode
                 select new ClusterResponseElement
                 {
                     ClusterId = c.SurveyClusterID,
                     ClusterCode = c.SurveyClusterCode,
                     ClusterName = c.SurveyClusterName,
                     Target = c.Targetweight.Value,
                     Variables =
                     (from v in c.SurveyClusterVariables
                      group v by v.MasterVariableID into g
                      let m = masterVariables.FirstOrDefault(x => x.MasterVariableID == g.Key)
                      select new ClusterVariableResponseElement
                      {
                          MasterVariableId = m.MasterVariableID,
                          MasterVariableCode = m.MasterVariableCode,
                          MasterVariableName = m.MasterVariableName,
                          Values = (
                            from vv in g
                            let vm = values.First(x => x.Map.MasterVariableID == vv.MasterVariableID && x.Value.ResponseOptionCode == vv.DistinctValue)
                            select new ClusterVariableValue {
                                Id = vv.SurveyClusterVariableID,
                                DistinctDisplay = vm.Value.ResponseOptionText,
                                Distinct = vm.Value.ResponseOptionCode,
                                From = vv.FromValue,
                                To = vv.ToValue
                            })
                      })


                 }).ToArray();

            return clusters;

I get a "Sequence has no matching elements error" on the LET clause 我在LET子句中收到“序列没有匹配的元素错误”

let vm = values.First(x => x.Map.MasterVariableID == vv.MasterVariableID && 
                           x.Value.ResponseOptionCode == vv.DistinctValue)

Any help is appreciated! 任何帮助表示赞赏! Im not too familiar with LINQ 我对LINQ不太熟悉

It means exactly that - you're asking LINQ to give you the first element of a sequence, but there aren't any. 这恰好意味着-您要LINQ给您序列的第一个元素,但没有任何元素。

Earlier in your query you're using FirstOrDefault instead. 在查询的前面,您使用的是FirstOrDefault You may consider using that here as well if it fits your use-case: 如果适合您的用例,您也可以在这里考虑使用它:

let vm = values.FirstOrDefault(x =>
    x.Map.MasterVariableID == vv.MasterVariableID
    && x.Value.ResponseOptionCode == vv.DistinctValue
)

Otherwise you'll have to re-work your query to be sure you're always getting a single element. 否则,您将必须重新处理查询以确保始终获得单个元素。

You can use First only if you are sure that the sequence contains elements, othwerwise you get this exception. 只有在确定序列包含元素的情况下才可以使用First ,否则将获得此异常。 I assume that you already know that since you have used also FirstOrDefault . 我假设您已经知道了,因为您还使用了FirstOrDefault So the question is how to get the ClusterVariableValue s in the query when it is empty, perhaps with FirstOrDefault and this conditional operator: 因此,问题是当它为空时如何在查询中获取ClusterVariableValue ,也许使用FirstOrDefault和此条件运算符:

...
let vm = values.FirstOrDefault(x => x.Map.MasterVariableID == vv.MasterVariableID && x.Value.ResponseOptionCode == vv.DistinctValue)
let cv = vm == null ? null : 
    new ClusterVariableValue {
        Id = vv.SurveyClusterVariableID,
        DistinctDisplay = vm.Value.ResponseOptionText,
        Distinct = vm.Value.ResponseOptionCode,
        From = vv.FromValue,
        To = vv.ToValue
    }
select cv   
....

Now ClusterVariableValue is null if there is no matching element in the sequence. 现在,如果序列中没有匹配的元素,则ClusterVariableValue为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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