问题描述
如果我在linqpad中使用以下代码来查询我的数据库,我得到了所需的结果:
LoadTables.Where(o=> o.Approver== "Name Name" ||o.Approver== "Name.Name").Select(o=>o.SubmittedBy).ToList().Distinct()但是,如果我修改它并将其放入我的代码,我会收到错误:
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.
我在哪里出错?
对于上下文,请参阅此处的任务:
目前它正在为每场比赛进行重新修整新表,我正试图让它返回每个用户的表.
推荐答案
从LINQ Expresion Select(o => o.SubmittedBy)中删除并在ToList()之前放置Distinct():
public IEnumerable<LoadTable> TableList; TableList = _context.LoadTable.Where(o => o.Approver == GetADDetails.displayName || o.Approver == GetADDetails.userName).Distinct().ToList();
其他推荐答案
您正在选择字符串属性
**.Select(o => o.SubmittedBy)**
此返回IEnumerable<string>
您需要在下面的内容
_context.LoadTable.Where(o => o.Approver == GetADDetails.displayName || o.Approver == GetADDetails.userName).Distinct().GroupBy(p => p.SubmittedBy).Select(grp => grp.FirstOrDefault());
其他推荐答案
alas您忘了告诉我们序列中的序列中的哪种对象是由_context.LoadTable返回的.查看您的代码,似乎它返回枚举对象序列,其中每个对象至少具有属性SubmittedBy.
查看错误,似乎SubmittedBy是一个字符串属性.
如果您已将代码拆分为较小的碎片并使用适当的标识符,您将很快看到您的问题.
让我们检查你的代码:
IEnumerable<LoadTable> TableList = _context.LoadTable .Where(o => o.Approver == GetADDetails.displayName || o.Approver == GetADDetails.userName) .Select(o => o.SubmittedBy) .ToList() .Distinct();
_context.LoadTable返回由我未知的IENUMAGERAGER的项目序列,因此让我们假设它是Notes的序列:
IEnumerable<Note> notes = _context.LoadTable;
可以是LoadTable返回一个IEnumerable而不是IEnumerabl<Note>,在这种情况下,您应该施放加载的表.
下一个陈述:
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的序列.
问题是:你想要唯一的LoadTables,或者你想要每个提交人的所有帖子提交的笔记吗?在这种情况下,您必须进入Groupby而不是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<...> a 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 firstordfault , or take(3).tolist()`,它将浪费处理能力以创建完整列表
问题描述
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();
The error returned is:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.IEnumerable<App.Models.LoadTable>' An explicit conversion exists.
Where am I going wrong?
For context, see previous quested here:
Using LINQ to loop through data and display in a table
Currently it's returing a new table for every match, I'm trying to get it to return a table per user.
推荐答案
Remove from the Linq expresion Select(o => o.SubmittedBy) and put Distinct()before of ToList():
public IEnumerable<LoadTable> TableList; TableList = _context.LoadTable.Where(o => o.Approver == GetADDetails.displayName || o.Approver == GetADDetails.userName).Distinct().ToList();
其他推荐答案
You are selecting a string property
**.Select(o => o.SubmittedBy)**
this return IEnumerable<string>
you need something like below
_context.LoadTable.Where(o => o.Approver == GetADDetails.displayName || o.Approver == GetADDetails.userName).Distinct().GroupBy(p => p.SubmittedBy).Select(grp => grp.FirstOrDefault());
其他推荐答案
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.
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 returns an IEnumerable sequence of items unknown by me, so let's assume it is a sequence of Notes:
IEnumerable<Note> notes = _context.LoadTable;
It could be that LoadTable returns an IEnumerable instead of IEnumerabl<Note>, in that case you should cast the loaded table.
The next statements:
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();
It is easy to see that a sequence of strings can't easily be converted to a sequence of LoadTables.
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(), });
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