C# 在LINQ中使用SQL distinct函数

C# 在LINQ中使用SQL distinct函数,c#,linq,C#,Linq,我想创建一个列表(MyList),其中每个IDNumber和SQL表(TABLE01)中的PersonName只对应一行。这是怎么用LINQ写的。使用下面的代码,我得到“不支持指定的方法”- **Jon的完整堆栈跟踪: at Devart.Data.Linq.LinqCommandExecutionException.CanThrowLinqCommandExecutionException(String message, Exception e) at Devart.Data.

我想创建一个列表(MyList),其中每个IDNumber和SQL表(TABLE01)中的PersonName只对应一行。这是怎么用LINQ写的。使用下面的代码,我得到“不支持指定的方法”-

**Jon的完整堆栈跟踪:

    at     Devart.Data.Linq.LinqCommandExecutionException.CanThrowLinqCommandExecutionException(String message, Exception e) at Devart.Data.Linq.Provider.DataProvider.ExecuteQuery(CompiledQuery compiledQuery, Object[] parentArgs, Object[] userArgs, Object lastResult)    
at Devart.Data.Linq.Provider.DataProvider.ExecuteAllQueries(CompiledQuery compiledQuery, Object[] userArguments)
   at Devart.Data.Linq.Provider.DataProvider.CompiledQuery.Devart.Data.Linq.Provider.ICompiledQuery.Execute(IProvider provider, Object[] userArgs)
  at Devart.Data.Linq.DataQuery`1.i()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Form1.Create_Modelist() in D:\projects\Form1.cs:line 488
at Form1.Form1_Load(Object sender, EventArgs e) in D:\projects\Form1.cs:line 565
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at .Program.Main() in D:\projects\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
如果您希望每个ID都有一个单独的名称,请使用Jon的答案。如果您希望将不同的ID/名称放在一起,则将不同的ID/名称放在列表中,而不是放在单个属性中

var MyList = (from a in TABLE01
                    where a.IDNumber != " "
                    select new
                               {
                                   Num = a.IDNumber,
                                   Name = a.PersonName
                               }).Distinct().ToList();

我怀疑问题在于它将
IDNumber
视为
IEnumerable
(因为
string
实际上是一个字符序列)。您可能想要更像:

var list = (from a in TABLE01
            where a.IDNumber != " "
            group a.Name by a.IDNumber into g
            select new { Num = g.Key, Name = g.First() }).ToList();

这基本上是按IDNumber进行分组(这样每个数字只能得到一个组),然后从每个组中取“第一个”(即一些任意的)名称。

这使得IDNumber和PersonName的组合不同。当他把distinct only放在IDNumber上时,可能是他只想要distinct,在这种情况下需要使用case组。@AndyNichols说得对。看起来Jon已经用分组讨论过这个场景了。我有一个2700万行的列表,其中有几行具有相同的ID和名称。我只希望每个ID号都有一行名称(第一行可以,最后一行最好,基于行数)…您是想通过ID号和PersonName区分,还是仅通过其中一个区分?你的问题不清楚,对不起。按IDNumber区分。@KasperØstergaardSixhøj:我强烈怀疑错误包含的信息比这个多。@KasperØstergaardSixhøj:我仍然怀疑还有更多信息-请将完整堆栈跟踪,包括任何内部异常和消息编辑到您的问题中(指定这是尝试我的答案的结果)@KasperØstergaardsishøj:好吧,你已经从中删除了类型和消息,这没有帮助。不过,它告诉我们您使用的是哪个LINQ提供程序,这在以前是不清楚的。在这一点上,除了其他,我建议您尝试编写一个小型控制台应用程序,它只包含此代码以及初始化数据库连接所需的任何内容。这将使堆栈轨迹更小…我将试一试Jon。非常感谢!:-)
var list = (from a in TABLE01
            where a.IDNumber != " "
            group a.Name by a.IDNumber into g
            select new { Num = g.Key, Name = g.First() }).ToList();