简体   繁体   English

C#如何使用Lambda表达式将iGrouping返回到列表

[英]C# How do i return iGrouping to a list using lambda expression

I am using entity framework to do this , i have this lambda expression : 我正在使用实体框架来做到这一点,我有这个lambda表达式:

public IList<Model.questionhint> GetRecordsPlease(int listTask, int listActivity)
{
   IList<Model.questionhint> lstRecords = context.questionhints.ToList();
   return lstRecords.GroupBy(x => new { 
                                      x.QuestionNo, 
                                      x.ActivityID, 
                                      x.TaskID })
                    .Where(a => a.Key.TaskID == listTask 
                             && a.Key.ActivityID == listActivity)
                    .ToList();         
}

but it says : 但它说:

Cannot implicitly convert type ' System.Collections.Generic.List<System.Linq.IGrouping<AnonymousType#1,iStellar.Model.questionhint>> ' to ' System.Collections.Generic.IList<iStellar.Model.questionhint> '. 无法将类型' System.Collections.Generic.List<System.Linq.IGrouping<AnonymousType#1,iStellar.Model.questionhint>> '隐式转换为' System.Collections.Generic.IList<iStellar.Model.questionhint> '。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)

This codes is in my CRUD class file named DAOQuestionHint . 这段代码位于我的CRUD类文件中,该文件名为DAOQuestionHint。

How do i return this because i need to call it in my xaml.cs ( code behind ) like this : 我该如何返回它,因为我需要在我的xaml.cs(后面的代码)中调用它,如下所示:

private DAO.DAOQuestionHint qh = new DAO.DAOQuestionHint(); 私有DAO.DAOQuestionHint qh =新的DAO.DAOQuestionHint();

IList<Model.questionhint> lstQuestionHints = qh.GetRecordsPlease(taskID, activityID);

This is how my database table looks like : 这是我的数据库表的样子:

在此处输入图片说明

what i wanted is to group records of the same activityID , taskID and questionNo together in a line , now every record is seperated line by line like this : 我想要的是将同一activityID,taskID和questionNo的记录分组在一行中,现在每个记录都像这样逐行分隔:

在此处输入图片说明

i want it to be like 我希望它像

I [answer] to have a nap every afternoon 我[回答]每天下午小睡

The sun [answer] not move round the earth. 太阳[answer]不能绕地球移动。

So i tried to use alternative in code behind by using for loop but that doesn't work perfectly if there are 3 or more records of the same activityID , taskID and questionNo , so i tot i need to group them in the query first . 所以我试图通过for循环在代码后面使用替代方法,但是如果有3条或更多记录具有相同的activityID,taskID和questionNo,那么这将不能完美地工作,所以我要先将它们分组到查询中。

It seems to me that you don't need to group at all - you just need to filter. 在我看来,您根本不需要分组-您只需要过滤即可。 Unless you really have more than one hint per question (with the same task and activity), you just need: 除非每个问题确实有多个提示(具有相同的任务和活动),否则您只需要:

public IList<Model.questionhint> GetRecordsPlease(int listTask, int listActivity)
{
    return context.questionhints
                  .Where(a => a.TaskID == listTask && a.ActivityID == listActivity)
                  .ToList();
}

Note that unlike your current code, this will perform the filtering in the database, rather than pulling the whole table to the client and then grouping/filtering. 请注意,与您当前的代码不同,这将在数据库中执行过滤,而不是将整个表拉到客户端,然后进行分组/过滤。 You need to be aware that any time you use context.SomeTable.ToList() , that will pull the entire contents of that table. 您需要知道, context.SomeTable.ToList()使用context.SomeTable.ToList()时,都会拉取该表的全部内容。

I'd also strongly suggest that you change your model to use type names which follow .NET naming conventions, so QuestionHint instead of questionhint . 我也强烈建议您将模型更改为使用遵循.NET命名约定的类型名称,因此使用QuestionHint而不是questionhint

(Finally, I'd remove the Please from the method name - computers don't need you to be polite in your naming like this.) (最后,我将从方法名称中删除“ Please ,因为计算机不需要您这样礼貌地待人。)

EDIT: If you really do want groups, you need to change the return value. 编辑:如果你真的想群体,就需要改变返回值。 For example, you could just return a sequence of groups: 例如,您可以只返回一组组:

public IList<IGrouping<int, Model.questionhint>>
     GetRecordsPlease(int listTask, int listActivity)
{
    return context.questionhints
                  .Where(a => a.TaskID == listTask && a.ActivityID == listActivity)
                  .ToList()
                  .GroupBy(a => a.QuestionNo)
                  .ToList();
}

I've deliberately called ToList() here before the grouping and then again afterwards so that the data is fetched once and you can iterate over those groups however you like without it going back to the database. 我在分组之前故意在此处调用ToList() ,然后在分组之后再次调用ToList() ,以便一次获取数据,并且可以随意遍历这些组,而无需将其返回数据库。 (It's possible that just the final ToList would be enough, but I wouldn't like to say for sure.) (有可能仅是最终的ToList就足够了,但是我不想肯定地说。)

暂无
暂无

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

相关问题 如何在 c# 中返回代表 function 或 lambda 表达式? - How do I return a delegate function or a lambda expression in c#? 如何在 C# 的字符串列表中添加 Lambda 表达式 - How do I prepend every item in a list of strings in C# with a Lambda Expression C#:如何操纵List <String> 使用LINQ或LAMBDA表达式 - C#: How to manipulate List<String> using LINQ or LAMBDA expression 如何将 C# lambda 表达式转换为委托? - How do I convert a C# lambda expression into a delegate? Lambda帮助在C#中进行分组和汇总,需要一个Func <IGrouping<int, anonymous type, int> 或表达 <Func<IGrouping<int, anonymous type> ,十进制&gt;&gt; - Lambda Help Grouping and Summing in C#, need a Func<IGrouping<int, anonymous type, int> or Expression<Func<IGrouping<int, anonymous type>, decimal>> 如何在C#2.0中重写lambda表达式? - How do I rewrite a lambda expression in C# 2.0? 如何在C#中创建此表达式树(动态lambda)? - How do I create this expression tree (dynamic lambda) in C#? 如何以编程方式创建 C# Lambda 表达式? - How Do I Create a C# Lambda Expression Programmatically? 如何使用lambda表达式过滤C#中的列表? - How to filter a list in C# with lambda expression? C#: How do I use a method, check if return is null, and set value in one line using lambda and C# Null Propagation - C#: How do I use a method, check if return is null, and set value in one line using lambda and C# Null Propagation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM