小编Mar*_*zek的帖子

for循环中的C#bug

我不应该得到负数,请看下面的截图:

见下图:

在此输入图像描述

这是代码:

    for (double i=8.0; i<=12;i=i+0.5)
        {
          double aa=  (i - Convert.ToInt32(i)) ;
          Console.WriteLine(" "+i+" "+aa);
        }
Run Code Online (Sandbox Code Playgroud)

c# console for-loop visual-studio-2010

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

在字典C#中排序

我有一本字典

Dictionary<string, string> rList = new Dictionary<string, string>();
rList .Add("/a/b/c", "35");
rList .Add("/a/c/f/v", "25");
rList .Add("/a/r/d/c/r/v", "29");
rList .Add("/a", "21");
rList .Add("/a/f, "84");
Run Code Online (Sandbox Code Playgroud)

我只想根据密钥中存在的"/"数来对此字典进行排序.我的预期出局是,

("/a/r/d/c/r/v", "29")
("/a/c/f/v", "25")
("/a/b/c", "35")
("/a/f, "84")
("/a", "21")
Run Code Online (Sandbox Code Playgroud)

.net c# vb.net asp.net

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

从没有 For Each 循环的 DataTable.Select 中获取第一个结果

下面的 For 循环根本不循环。在没有 For 循环的情况下,是否有任何优化的方法可以做到这一点:

For Each drID As DataRow In dttable.Select("ID=1 and FirstName='Karthik'", "ID")
    NewID = CInt(drID.Item("ID"))
    Exit For
Next
Run Code Online (Sandbox Code Playgroud)

我试过用

NewID = IIf(dt.Select("ID=1 and FirstName='Karthik'", "ID").Length > 0, dt.Select("ID=1 and FirstName='Karthik'", "ID")(0).Item("ID"), 0)
Run Code Online (Sandbox Code Playgroud)

有没有其他优化的方法来改变这个根本不循环的 For 循环。

vb.net

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

泛型可以用来返回实际对象吗?

我有这段代码:

private object DeserialiseFromXMLFile(string fileLocation, Type type)
{
    XmlSerializer serializer = new
        XmlSerializer(type);

    FileStream fs = new FileStream(fileLocation, FileMode.Open);
    XmlReader reader = new XmlTextReader(fs);
    return serializer.Deserialize(reader);
}
Run Code Online (Sandbox Code Playgroud)

我想知道我是否使用泛型,因为我希望返回类型为T

有谁知道这是可能的还是这是最优雅的解决方案?

提前致谢

c# generics

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

C#使用反射与通用属性

我有一个使用通用属性的类.例如:

class Person
{
    public MyGenericProperty<string> Field1
    {
        get { return field1; }
        set { field1 = value; }
    }

    private MyGenericProperty<string> field1= new MyInheritedGenericProperty<string>("Alan1");
}
Run Code Online (Sandbox Code Playgroud)

我想在另一个类中使用这个类和反射,我有一个类似的方法

public void DoSomethingWithProperty(object sourceobject)
{
    foreach (var aProperty in sourceobject.GetType().GetProperties())
    {
        *if(aProperty.PropertyType == typeof(MyGenericProperty<>))*
        {
           *var obj = (MyGenericProperty<>)aProperty.GetValue(sourceobject, null);*
        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

我有两个问题

1-如何进行通用属性的类型检查.在那个示例代码中if(aProperty.PropertyType == typeof(MyGenericProperty<>))不起作用.

MyGenericProperty的2-T可以是任何类,如何在不通过反射知道T的情况下转换MyGenericProperty类

var obj = (MyGenericProperty<>)aProperty.GetValue(sourceobject, null);
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

c# generics reflection

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

使用Linq语句选择intellisense中缺少的方法

我可能真的忽略了一些东西,但是当使用Linq to Entities linq语句创建一个类时,intellisense中缺少Select方法!

我正在使用Visual Studio 2013和EntityFrameWork 6.0.2.

我有以下代码片段,intellisense中下一个最接近的方法是RemoveRangeSingleAsync.介于两者之间我应该找到Select!

我想从Ratings类中获取一部分属性,而不是全部.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Threading.Tasks;
using AjaxCallModel;
using AjaxCallModel.ViewModels;

namespace WcfServiceLibraryAjaxCall
{
    public class AjaxCall : IAjaxCall
    {
        public async Task<List<Rating>> SelectRatingsAsync()
        {
            try
            {
                using (BillYeagerEntities DbContext = new BillYeagerEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
                    DbContext.Database.Connection.Open();

                    var ratings = await DbContext.Ratings.ToListAsync();

                    return ratings;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Run Code Online (Sandbox Code Playgroud)

如果我将它与我的另一个项目进行比较,则没有问题(请参阅下面的代码).

有人知道我做错了什么或错过了吗?

using System;
using System.Collections.Generic;
using …
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework

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

我为什么要将List转换为Array

查看以下代码行:

public string[] GetStringList(params object[] values)
{
    List<string> _stringList = new List<string>();

    foreach (object value in values)
    {
        _stringList.Add(Convert.ToString(value));

        //Do something
    }

    //Why this invalid?
    return _stringList;

    //Why do required to convert list collection in array
    return _stringList.ToArray();
}
Run Code Online (Sandbox Code Playgroud)

由于列表和数组都是集合,那么为什么我需要将List转换为数组呢?

c# arrays list

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

Linq to Entities Datetime 错误

我有以下代码

var dates = query.Select(
                 x => DateTime.ParseExact(x.Date, "yyyy-MM", CultureInfo.InvariantCulture));

var minDate = dates.Min(x => x);
Run Code Online (Sandbox Code Playgroud)

但是当我执行它时,我得到了异常

System.Data.Entity.dll 但未在用户代码中处理

其他信息:LINQ to Entities 无法识别“System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)”方法,并且此方法无法转换为存储表达式。

我究竟做错了什么?我该如何解决这个问题?

c# sql linq datetime entity-framework

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

C#中通用接口的简写语法

T?Nullable<T>Eg int?的简写:简写Nullable<int> 是简写语法是语言规范的一部分吗?

我们是否可以为通用接口创建自己的速记语法?

.net c#

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

检查xmlReader是否是具体的结束元素

我已经设置了xmlReader并且可以检查某些元素,但是我找不到检查关闭元素的方法,让我说</perls>除了它的开放之外我还想要另外一个case语句,我怎么能这样做?我确信这样的标签不是自动关闭的.

using (XmlReader reader = XmlReader.Create("perls.xml"))
{
    while (reader.Read())
    {
    // Only detect start elements.
    if (reader.IsStartElement())
    {
        // Get element name and switch on it.
        switch (reader.Name)
        {
        case "perls":
            // Detect this element.
            Console.WriteLine("Start <perls> element.");
            break;
        case "article":
            // Detect this article element.
            Console.WriteLine("Start <article> element.");
            // Search for the attribute name on this current node.
            string attribute = reader["name"];
            if (attribute != null)
            {
            Console.WriteLine("  Has attribute name: " + attribute);
            }
            // Next …
Run Code Online (Sandbox Code Playgroud)

c# xml xmlreader

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