小编Rit*_*ita的帖子

Linq按顺序分组,每组按顺序排序?

我有一个看起来像这样的对象:

public class Student
{
    public string Name { get; set; } 
    public int Grade { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想创建以下查询:按学生姓名分组成绩,按成绩对每个学生组进行排序,并按每组中的最高成绩对订单组进行排序.

所以它看起来像这样:

A 100
A 80
B 80
B 50
B 40
C 70
C 30
Run Code Online (Sandbox Code Playgroud)

我创建了以下查询:

StudentsGrades.GroupBy(student => student.Name)
    .OrderBy(studentGradesGroup => studentGradesGroup.Max(student => student.Grade));
Run Code Online (Sandbox Code Playgroud)

但是返回IEnumerable IGrouping,我无法对列表进行排序,除非我在另一个foreach查询中执行此操作并使用将结果添加到其他列表AddRange.

有更漂亮的方法吗?

c# linq group-by sql-order-by

61
推荐指数
4
解决办法
12万
查看次数

记录到文件或事件查看器?

我想知道记录信息消息的"正确"方法是什么; 到文件,或在事件查看器中的特殊日志?

我喜欢记录到文件,因为我可以使用滚动平面文件监听器并查看每天的新记录,而且在事件查看器中我一次只能看到一条消息 - 在文件中我可以很容易地扫描一天.我的同事争辩说,文件占用空间,他喜欢在一个地方发出警告,错误和信息消息.你怎么看?有首选方式吗?如果是这样,为什么?

此外,任何方法中是否存在并发问题?我已经读过entlib是线程安全的,如果监听器不是线程安全的话,会生成一个Monitor.Enter,但我想确定(我们只是使用Logger.Write).我们正在使用entlib 3.1.

先感谢您.

.net c# logging enterprise-library thread-safety

8
推荐指数
1
解决办法
2617
查看次数

在错误的域中模拟用户不会引发异常

我使用了常见的模拟代码,它工作得很好,直到我在域中插入随机的'dggdgsdg' - 然而它仍然有效...

if (LogonUser(Username, Domain, Password, Logon32LogonInteractive, Logon32ProviderDefault, ref existingTokenHandle) &&
    DuplicateToken(existingTokenHandle, (int)SecurityImpersonationLevel.SecurityDelegation, ref duplicateTokenHandle))
    {
            Identity = new WindowsIdentity(duplicateTokenHandle);
            ImpersonationContext = Identity.Impersonate();
    }
    else
    {
            throw new Win32Exception(Marshal.GetLastWin32Error());
    } 
Run Code Online (Sandbox Code Playgroud)

我在我的域上使用了一些TestUser,但它确实有效.我然后切换域,随意废话'werwerhrg',它冒充我的域名上的TestUser!为什么?我会期待抛出异常,为什么它在起作用?

private const int Logon32LogonInteractive = 2;
private const int Logon32ProviderDefault = 0;

public enum SecurityImpersonationLevel
        {
            SecurityAnonymous = 0,
            SecurityIdentification = 1,
            SecurityImpersonation = 2,
            SecurityDelegation = 3
        }
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, …
Run Code Online (Sandbox Code Playgroud)

.net c# impersonation identity

5
推荐指数
1
解决办法
1056
查看次数

子对话框窗口上的FindWindowEx

我正在尝试获取子对话框窗口的句柄。我试过使用FindWindowEx,但没有用。相反,FindWindow确实起作用。

我使用以下代码对Visual Studio的“选项”窗口进行了实验:

IntPtr vsHandle = Process.GetProcessById(vsProcessId).MainWindowHandle; // consistent with spy++'s parent handle of options window

IntPtr optionsHandle = FindWindowEx(vsHandle, IntPtr.Zero, "#32770", "Options"); // returns 0

IntPtr optionsHandle2 = FindWindow("#32770", "Options"); // returns correct handle
Run Code Online (Sandbox Code Playgroud)

据我了解,FindWindowEx应该已经工作了,它是一个子窗口。

我正在运行Windows XP,并且还尝试使用FindWindowEx(vsHandle,IntPtr.Zero,“#32770”,null)。没用 似乎唯一的获得方法是使用FindWindow,因为打开具有相同对话框的两个父实例并不足够。

这是声明:

[DllImport("user32.dll")]
Private static extern IntPtr FindWindow(string className, string windowTitle);
[DllImport("user32.dll")]
Private static extern IntPtr FindWindowEx(IntPtr parentHWnd, IntPtr childAfterHWnd,     string className, string windowTitle);
Run Code Online (Sandbox Code Playgroud)

提前致谢。

c# winapi findwindowex

2
推荐指数
1
解决办法
7322
查看次数

Linq to xml,检索基于通用接口的列表

我有一个看起来像这样的XML文档

    <Elements>
     <Element>
      <DisplayName />
      <Type />
     </Element>
    </Elements>
Run Code Online (Sandbox Code Playgroud)

我有一个界面,

interface IElement
{
    string DisplayName {get;}
}
Run Code Online (Sandbox Code Playgroud)

以及一些派生类:

public class AElement: IElement

public class BElement: IElement
Run Code Online (Sandbox Code Playgroud)

我想要做的是编写最有效的查询来迭代XML并创建一个列表IElement,包含AElementBElement基于XML中的"Type"属性.

到目前为止我有这个:

IEnumerable<AElement> elements = 
    from xmlElement in XElement.Load(path).Elements("Element")
    where xmlElement.Element("type").Value == "AElement"
    select new AElement(xmlElement.Element("DisplayName").Value);

return elements.Cast<IElement>().ToList();
Run Code Online (Sandbox Code Playgroud)

但这只是为了AElement.有没有办法添加BElement相同的查询,并使其通用IEnumerable<IElement>?或者我是否必须为每个派生类型运行此查询一次?

c# xml linq

1
推荐指数
1
解决办法
633
查看次数

GetChildAtPoint仅返回禁用的控件?

我正在使用以下示例在禁用按钮上方显示工具提示. 如何在禁用按钮上显示工具提示?

按钮位于面板内,我将MouseMove事件附加到面板,而不是表单本身.令人惊讶的是,如果启用该按钮,GetChildAtPoint将返回null!该按钮仅在禁用时返回.我已经检查了按钮的位置,它在启用和禁用时是相同的,并且它的父节点都是面板.

要清楚; 在所有控件上调用MouseMove事件,在其中有一个用法在GetChildAtPoint方法中.启用该按钮后,将调用MouseMove,GetChildAtPoint将返回null.禁用时,将调用MouseMove,GetChildAtPoint将返回该按钮.

我不明白为什么这段代码有效.非常感谢任何帮助,谢谢.

c# tooltip winforms

1
推荐指数
1
解决办法
1304
查看次数