是的,所以我有一个可枚举的,并希望从中获得不同的值.
使用System.Linq
,当然有一个名为的扩展方法Distinct
.在简单的情况下,它可以在没有参数的情况下使用,例如:
var distinctValues = myStringList.Distinct();
Run Code Online (Sandbox Code Playgroud)
好的,但如果我有一个可以指定相等性的可枚举对象,唯一可用的重载是:
var distinctValues = myCustomerList.Distinct(someEqualityComparer);
Run Code Online (Sandbox Code Playgroud)
equality comparer参数必须是.的实例IEqualityComparer<T>
.当然,我可以做到这一点,但它有点冗长,而且很有说服力.
我所期望的是一个需要lambda的重载,比如Func <T,T,bool>:
var distinctValues
= myCustomerList.Distinct((c1, c2) => c1.CustomerId == c2.CustomerId);
Run Code Online (Sandbox Code Playgroud)
任何人都知道是否存在某些此类扩展或某些等效的解决方法?或者我错过了什么?
或者,有没有一种方法可以指定IEqualityComparer内联(embarass me)?
更新
我找到了Anders Hejlsberg对MSDN论坛中关于这个主题的帖子的回复.他说:
您将遇到的问题是,当两个对象比较相等时,它们必须具有相同的GetHashCode返回值(否则Distinct内部使用的哈希表将无法正常运行).我们使用IEqualityComparer,因为它将Equals和GetHashCode的兼容实现打包到一个接口中.
我认为那是有道理的..
在他出色的着作"CLR Via C#"中,杰弗里里希特说他不喜欢属性,并建议不要使用它们.他给出了一些理由,但我并不理解.任何人都可以向我解释为什么我应该或不应该使用属性?在C#3.0中,具有自动属性,这会改变吗?
作为参考,我添加了Jeffrey Richter的观点:
•财产可以是只读的或只写的; 字段访问始终是可读写的.如果定义属性,最好同时提供get和set访问器方法.
•属性方法可能会抛出异常; 字段访问永远不会抛出异常.
•属性不能作为out或ref参数传递给方法; 一个领域可以.例如,以下代码将无法编译:
using System;
public sealed class SomeType
{
private static String Name
{
get { return null; }
set {}
}
static void MethodWithOutParam(out String n) { n = null; }
public static void Main()
{
// For the line of code below, the C# compiler emits the following:
// error CS0206: A property or indexer may not
// be passed as an out or ref parameter
MethodWithOutParam(out Name);
} …
Run Code Online (Sandbox Code Playgroud) 我有一个包含以下内容的枚举(例如):
在我的代码中,我使用Country.UnitedKingdom,但是如果我将它分配给字符串,我希望将值设置为UK.
这可能吗?
当您将鼠标悬停在该特定行中的项目上时,如何显示datagridview
每个项目的工具提示datagridview
?
我有表格product
列:
product name
product price
product description
product image ....
Run Code Online (Sandbox Code Playgroud)
我有一个要求,我有一个datagridview
列,我从数据库中获取这些:
product name
product price
product image ....
Run Code Online (Sandbox Code Playgroud)
现在我想显示这样的工具提示:如果我将鼠标悬停在产品图像上,将显示该产品的产品说明.我想为每一行做这件事.有人请帮忙吗?
我使用下面的方法来获取文件名..
但它返回整个路径....现在我不想得到整个路径..
我只想要文件名而不是整个路径......
我怎么能得到只有文件名而不是整个路径
path = c:\ docs\doc\backup-23444444.zip
string[] filenames = Directory.GetFiles(targetdirectory,"backup-*.zip");
foreach (string filename in filenames)
{ }
Run Code Online (Sandbox Code Playgroud)
任何人都会对此有所帮助.....
非常感谢...
我有一个products
用列调用的表:
productid ,
productname,
productprice
categoryid
Run Code Online (Sandbox Code Playgroud)
我的问题是我想根据产品名称和细节获得产品数量.我想显示数据DataGridView
.我如何知道单个产品名称的产品数量,如下所示?
productid productname productavailable productprice
--------------------------------------------------------------------------
1 product A 2 products(product A) 100
2 product B 5 Products(product B) 200
Run Code Online (Sandbox Code Playgroud)
像上面的表我必须显示DataGridView
.我正在使用LINQ和C#,我的DbContext
名字是tsgdbcontext
.
我正在尝试使用asp.net Repeater构建一个表结构,如下所示:
column 1 | Column 2
Row1 cell1 cell2
---------------------------------------
TABLE 1 TABLE 2
----------------------------------
col1|Col2|Col3_ same column and rows are here as well
Row2 row1____|____|____
row2___ |____|_____
row3____|____|_____
Run Code Online (Sandbox Code Playgroud)
但我在第2行添加表1和表2时遇到困难.我不知道如何在Repeater内的单个单元格中添加表格,数据需要从DataTable绑定.
以下是我的Repeater代码:
<asp:Repeater ID="Repeaterp" runat="server">
<HeaderTemplate>
<table>
<tr><th>usedcount</th><th>notUsedCount</th></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:TextBox runat="server" ID="txtAvai" Text='<%#Eval("Count") %>' ReadOnly="true"></asp:TextBox></td>
<td><asp:TextBox runat="server" ID="txtConv" Text='' ReadOnly="true"></asp:TextBox></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater
Run Code Online (Sandbox Code Playgroud)
请问任何一个人能否对这个非常感激我的想法?
我有一个datagrid视图,我想删除所有行的行标题...
我试图删除圆角部分,如下图所示(行标题)
为此,我已经编写了下面的代码......但它并没有开始......
private void dgvProducts_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
e.PaintCells(e.ClipBounds, DataGridViewPaintParts.All);
e.PaintHeader(DataGridViewPaintParts.All
| DataGridViewPaintParts.Border
| DataGridViewPaintParts.None
| DataGridViewPaintParts.SelectionBackground
| DataGridViewPaintParts.ContentBackground);
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
任何请给出任何示例代码来删除datagrid视图中的行标题.....
提前谢谢了.....
如何使用linq将字符串转换为日期时间到实体....
我有以下查询,其中visit_date
列数据类型是字符串...
var memberl = from v in abc.visits
join m in abc.members on v.member_Id equals m.member_Id
where Convert.ToDateTime(v.visit_Date) >= startdate &&
Convert.ToDateTime(v.visit_Date) <= enddate
group m by new { m.member_Firstname,
m.member_Lastname, m.member_Id } into g
orderby g.Count()
select new
{
numVisits = g.Count(),
firstname = g.Key.member_Firstname,
lastname = g.Key.member_Lastname
};
Run Code Online (Sandbox Code Playgroud)
不幸的是我无法改变架构......
我有错误:
linq to entites does not recognise Convert.ToDatetime method
Run Code Online (Sandbox Code Playgroud)
是否有任何可能的解决方案将字符串转换为Datetime?
更新的代码:
根据要求我更新了我的问题
var data = (from v in abc.visits
join m in abc.members on v.member_Id equals …
Run Code Online (Sandbox Code Playgroud) 如何将其转换hexa code = #2088C1
为颜色名称像蓝色或红色
我的目标是为了给定的六进制代码,我想获得类似"蓝色"的颜色名称
我尝试了以下代码,但它没有给出任何颜色名称..
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#2088C1");
Color col = ColorConverter.ConvertFromString("#2088C1") as Color;
Run Code Online (Sandbox Code Playgroud)
但它没有给出这样的颜色名称"aquablue"
我正在使用带有c#的winforms应用程序