繁体   English   中英

使用 Linq 获得不同的属性

[英]Get Distinct Properties Using Linq

我有一个 object 列表,我必须在其中显示不同的属性,列表如下:

public class Questions
{
    public Nullable<int> QuestionId { get; set; }
    public string QuestionName { get; set; }
    public string Options { get; set; }
}

public List<Questions> GetLst()
{
    List<Questions> aLst = new List<Questions>()
    {
        new Questions() { QuestionId = 1, QuestionName = "What is the capital of England?", Options = "London" },
        new Questions() { QuestionId = 1, QuestionName = "What is the capital of Engand?", Options = "Jakarta" },
        new Questions() { QuestionId = 2, QuestionName = "Who invented computer?", Options = "Thomas Edison" },
        new Questions() { QuestionId = 2, QuestionName = "Who invented computer?", Options = "Charles Babbage" },
    };

    return aLst;
}

因此可以看出,在列表中有重复的属性值,如QuestionIdQuestionName 所以在前端,我使用Razor来显示列表,并且简单地将循环迭代为给定 object 的数量。

 @foreach (var item in Model.Distinct())          
 {
     <div class="heading">
        <div class="h2Val">
            @item.QuestionId
        </div>
     <div>@item.QuestionName</div>

     @foreach (var item2 in Model.Where(c => c.QuestionId == item.QuestionId))
     {
         <div class="heading2">
           <div>
             <input type="checkbox" class="cbCheck" value="@item2.Options" />@item2.Options
           </div>
         </div>
     }
     <div>
       <input type="button" class="btn" value="Get Value" />
     </div>
    </div>
 }

所以我预期的 output 如下:

1
What's the capital of England?
Option 1: London
Option 2: Jakarta

我现在得到的:

1
What's the capital of England?
Option 1: London

1
What's the capital of England?
Option 2: Jakarta

我尝试了类似这样的方法来区分属性值,如下所示,但它根本没有帮助:(使用RazorLinq解决它的任何更好的主意)

aLst = db.Questions.DistinctBy(p => new { p.QuestionId, p.QuestionName, p.Options }).ToList(); 
 @foreach(var item in Model.GroupBy(x=> x.QuestionId))          
 {
     <div class="heading">
        <div class="h2Val">
            @item.Key
        </div>
     <div>@item.FirstOrDefault()?.QuestionName</div>

     @foreach(var item2 in item.Select(x=>x.Options).ToList())
     {
         <div class="heading2">
           <div>
             <input type = "checkbox" class="cbCheck" value="@item2" />@item2
             </div>
         </div>
     }
     <div>
       <input type = "button" class="btn" value="Get Value" />
     </div>
    </div>
 }

您可以像这样使用 GroupBy:

        var questions = Questions.GetLst()
                         .GroupBy(q => new
                         {
                             q.QuestionId,
                             q.QuestionName
                         }, p => p.Options)
                         .Select(
                             grp => new
                             {
                                 Id = grp.Key.QuestionId,
                                 Name = grp.Key.QuestionName,
                                 Options = grp.ToList()
                             }
                         );

        foreach(var question in questions)
        {
            Console.WriteLine(question.Id);
            Console.WriteLine(question.Name);

            foreach(string option in question.Options)
            {
                Console.WriteLine(option);
            }
        }

我在控制台中对此进行了测试,output 是:

1
What is the capital of England?
London
Jakarta
2
Who invented computer?
Thomas Edison
Charles Babbage

暂无
暂无

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

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