结合使用LINQ和Select和Distinct方法

编程入门 行业动态 更新时间:2024-10-23 02:10:59
本文介绍了结合使用LINQ和Select和Distinct方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我在LINQPad中使用以下代码查询数据库,则会得到所需的结果:

If I use the following code in LINQPad to query my database I get the desired results:

LoadTables.Where(o=> o.Approver== "Name Name" ||o.Approver== "Name.Name").Select(o=>o.SubmittedBy).ToList().Distinct()

但是,如果我对此进行修改并将其放入代码中,则会收到错误消息:

However, if I amend this and put it into my code, I get an error:

public IEnumerable<LoadTable> TableList; TableList = _context.LoadTable.Where(o => o.Approver == GetADDetails.displayName || o.Approver == GetADDetails.userName).Select(o => o.SubmittedBy).ToList().Distinct();

返回的错误是:

无法隐式转换类型'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.IEnumerable<App.Models.LoadTable>' An explicit conversion exists.

我要去哪里错了?

有关上下文,请参见前面的问题:

For context, see previous quested here:

使用LINQ遍历数据和在表格中显示

当前,它为每个匹配都重新创建一个新表,我正在尝试让它为每个用户返回一个表.

Currently it's returing a new table for every match, I'm trying to get it to return a table per user.

推荐答案

A,您忘了告诉我们_context.LoadTable返回的序列中包含哪些对象.查看您的代码,似乎它返回了一个可枚举的对象序列,其中每个对象至少具有一个属性SubmittedBy.

Alas you forgot to tell us what kind of objects are in the sequence that is returned by _context.LoadTable. Looking at your code, it seems that it returns an enumerable sequence of objects, where every object has at least a property SubmittedBy.

看着您的错误,看来SubmittedBy是一个字符串属性.

Looking at your error, it seems that SubmittedBy is a string property.

如果将代码分成较小的部分并使用适当的标识符,您很快就会发现问题所在.

If you had split your code into smaller pieces and used proper identifiers, you would soon have seen your problem.

让我们检查一下您的代码:

Let's examine your code:

IEnumerable<LoadTable> TableList = _context.LoadTable .Where(o => o.Approver == GetADDetails.displayName || o.Approver == GetADDetails.userName) .Select(o => o.SubmittedBy) .ToList() .Distinct();

_context.LoadTable返回我未知的IEnumerable序列,所以我们假设它是Notes的序列:

_context.LoadTable returns an IEnumerable sequence of items unknown by me, so let's assume it is a sequence of Notes:

IEnumerable<Note> notes = _context.LoadTable;

LoadTable可能返回IEnumerable而不是IEnumerabl<Note>,在这种情况下,您应该转换已加载的表.

It could be that LoadTable returns an IEnumerable instead of IEnumerabl<Note>, in that case you should cast the loaded table.

下一条语句:

IEnumerableM<Note> notesApprovedByUser = notes .Where(note => note.Approver == GetADDetails.displayName || note.Approver == GetADDetails.userName); IEnumerable<string> submitters = notesApprovedByUser .Select(note => note.SubmittedBy); List<string> submitterList = submitters.ToList(); IEnumerable<string> distinctSubmitters = submitterList.Distinct();

很容易看出,不能轻易将字符串序列转换为LoadTables序列.

It is easy to see that a sequence of strings can't easily be converted to a sequence of LoadTables.

问题是:您想要唯一的LoadTables,还是要为每个提交者提供所有历史记录提交的注释?在这种情况下,您将必须Groupby而不是Select:

The question is: do you want unique LoadTables, or do you want for every submitter all hist submitted notes? In that case you'll have to Groupby instead of Select:

.Where(note => ...) .GroupBy(note => note.SubmittedBy, // make groups of Notes submitted by the same submitter // parameter resultSelector: take every submitter and all notes that // were submitted by this submitter to make a new object (submittedBy, notesSubmittedByThisSubmitter) => new { // select the properties you plan to use Submitter = submittedBy LoadTables = notesSubmittedByThisSubmitter.Select(note => new LoadTable { ... again: select the properties you need }) .ToList(), });

记住:保留IEnumerable<...>和IEnumerable<...> as long as possible. If not necessary, don't do a ToList()before you return. If your query returns 1000 items, and your caller will only do FirstOrDefault , or Take(3).ToList()`,创建完整列表会浪费处理能力

Remember: keep an IEnumerable<...> an IEnumerable<...> as long as possible. If not necessary, don't do aToList()before you return. If your query returns 1000 items, and your caller will only doFirstOrDefault, orTake(3).ToList()`, it would be a waste of processing power to create your complete list

更多推荐

结合使用LINQ和Select和Distinct方法

本文发布于:2023-10-26 23:30:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531658.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   LINQ   Select   Distinct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!