在linq中相交以返回不同的属性

时间:2013-07-08 05:48:41

标签: c# asp.net linq

我对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的列表,但我只获得用户ID和问题。

由于

母亲被定义为名单母亲;

答案包含答案'其定义为:

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;

}

2 个答案:

答案 0 :(得分:0)

您可以通过选择oneA这样的所有元素来获取所有字段

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();

答案 1 :(得分: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的答案ID为Q101b,AnswerValue字段中有一些内容。

下一个选择IDList中具有ID的所有母亲。