0

我有一个关于 linq 的查询:

public void method()              
{
var answers = Answers.GetAnswers;

var oneA = (from a in answers
                    where a.Questionid == "Q101b" && a.AnswerValue.Length > 0
                    select new QuestionInfo { userId = a.UserId, questionId = a.Questionid }).ToList();

        var mumsHabits = oneA.Intersect(mothers, new UserIdEqualityComparer()).ToList();

}

在我的方法中,我试图用 answerValue 获取所有 mumsHabits 的列表,但我只得到 userid 和 questionid。

谢谢

母亲被定义为列表母亲;

答案包含“答案”,其定义为:

public partial class Answer : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _AnswerId;

    private string _Questionid;

    private int _UserId;

    private string _AnswerValue;

    private string _SelectionValue;

    private System.DateTime _DateAnswered;

    private string _Question;

    private EntityRef<User> _User;

}
4

2 回答 2

0

这个怎么样?

var IDList = answers.Where(a => a.Questionid == "Q101b" && a.AnswerValue.Length > 0).Select(a => a.UserId).ToList();
var mumsHabits = mothers.Where(m => IDList.Contains(m._UserId)).ToList();

第一个获取所有 UserId,其中 Answer ID 为 Q101b,并且 AnswerValue 字段中有内容。

接下来选择在 IDList 中具有 ID 的所有母亲。

于 2013-07-08T05:55:12.313 回答
0

您可以通过将所有元素选择为oneALike this

var oneA = (from a in answers
                where a.Questionid == "Q101b" && a.AnswerValue.Length > 0
                select a).ToList();

var mumsHabits = oneA.Intersect(mothers, new UserIdEqualityComparer()).ToList();
于 2013-07-08T05:53:14.400 回答