小编Mar*_*zek的帖子

在数据表上使用 LINQ 进行内联接

我有这 2 个数据表,customerTableDT并且customerAliasesTableDT. 它们都是从数据库中填充的,如下所示:

customerTableDT = UtilityDataAndFunctions.PerformDBReadTransactionDataTableFormat(String.Format("SELECT * FROM {0}", TableNames.customers));

customerAliasesTableDT = UtilityDataAndFunctions.PerformDBReadTransactionDataTableFormat(String.Format("SELECT * FROM {0}", TableNames.customerAliases));
Run Code Online (Sandbox Code Playgroud)

现在我尝试对两个数据表进行内部联接,如下所示:

var customerNames = from customers in customerTableDT.AsEnumerable()
                    join aliases in customerAliasesTableDT.AsEnumerable on customers.Field<int>("CustomerID") equals aliases.Field<int>("CustomerID")
                    where aliases.Field<string>("Alias").Contains(iString) select customers.Field<string>("Name")
Run Code Online (Sandbox Code Playgroud)

但它给了我这个错误:

The type of one of the expressions in the join clause is incorrect.  Type inference failed in the call to 'Join'.
Run Code Online (Sandbox Code Playgroud)

如果我必须用 SQL 编写我想要做的事情,那么它非常简单:

SELECT * FROM CUSTOMERS C
INNER JOIN CustomerAliases ALIASES ON ALIASES.CustomerID = C.CustomerID …
Run Code Online (Sandbox Code Playgroud)

c# linq inner-join

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

将foreach循环转向Linq

无论如何都要将每个循环写入Linq表达式:

private List<string> datasetItemset;
Dictionary<string, int> itemsetScanning = new Dictionary<string, int>();
List<string> itemList = new List<string>();
foreach (string transaction in this.datasetItemset)
{
    string[] items = transaction.Split(new char[] { ' ' });
    foreach (string item in items)
        if (!itemList.Contains(item))
        {
            itemList.Add(item);
            itemsetScanning.Add(item, 0);
        }
}
Run Code Online (Sandbox Code Playgroud)

我的下一个问题是,使用linq表达式而不是foreach循环加速程序的性能,我对这个linq有点新意.

更新:使用太多的foreach循环会减慢我的程序速度.

c# linq

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

如何使用Linq to XML获取这些XML值?

我想要一个将返回IEnumerable字符串的内部查询

'as.m3''as.m4'

我曾尝试 xDoc.Elements("moduleid")xDoc.Descendents("moduleid")

没有运气

<?xml version="1.0" encoding="UTF-8">
<root>
    <code>M11088MUBWWLSRSV9LTJBH81QT</code>
    <moduleid>as.m3</moduleid>
    <moduleid>as.m4</moduleid>
</root>
Run Code Online (Sandbox Code Playgroud)

c# xml linq linq-to-xml

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

通用类型推断不考虑多态性

嗨,这是我的例子,让我们说

class A { }
class B : A { }
Run Code Online (Sandbox Code Playgroud)
void test<T>(T clazz)
{
     Console.WriteLine("clazz type = {0} T type = {1}",
                 clazz.GetType().Name,
                 typeof(T).Name);
}

static void Main(string[] args)
{         
    A b = new B(); 
    test(b);
    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

结果是clazz = BT = A ????? 为什么推理泛型类型不考虑多态性?

c# generics

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

Json在C#中反序列化

如何在Json下面使用JsonConvert.DeserializeObject

[{
  "attributes" : {
    "type" : "User",
    "url" : "/xx/xx/xx"
  },
  "Id" : "1",
  "Name" : "abc"
},{
  "attributes" : {
    "type" : "User",
    "url" : "/xx/xx/xx"
  },
  "Id" : "2",
  "Name" : "abc"
},{
  "attributes" : {
    "type" : "User",
    "url" : "/xx/xx/xx"
  },
  "Id" : "3",
  "Name" : "abc"
}]
Run Code Online (Sandbox Code Playgroud)

这些是我的班级

public class Attributes
{
    public string type { get; set; }
    public string url { get; set; }
}

public class RootObject
{
    public Attributes attributes …
Run Code Online (Sandbox Code Playgroud)

c# json json.net

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

C#:如何使用DateTime作为参数创建构造函数?以及如何使用此构造函数创建对象?

我创建了一个构造函数如下:

public Animal(string regNum, DateTime brought, string name)
{
    this.RegNumber = regNum;
    this.DateBrought = brought;
    this.Name = name;
    this.NameNewOwner = null;
}
Run Code Online (Sandbox Code Playgroud)

基于上面的构造函数,我创建了一个名为的对象pet,如下所示:

Animal pet = new Animal("a12344", Convert.ToDateTime(23/01/2013), "Fluffy");
Run Code Online (Sandbox Code Playgroud)

但是,当我运行我的程序时,它会给我一个错误说:Invalid cast from Int32 to DateTime任何人都可以帮我这个吗?

c# parameters datetime constructor object

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

使用Date列将日期插入到sql表中

您好,感谢您的阅读.

我正在尝试将当前日期插入到我的表中,但我无法弄清楚如何正确编写它.

这是我的C#代码:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
conn.Open();

string Comment = UserWriteComment.Text;
string ID = DetailedID.Text;
string Name = DetailedName.Text;
string UniqueID = lblID.Text;


string query = "INSERT INTO Comment(TicketID, Name, Comments, UserID, Date)" + "Values('" + ID + "', '" + Name + "', '" + Comment + "', '" + UniqueID + "', '" + Date + "')";
using (SqlCommand com = new SqlCommand(query, conn))
{
    com.ExecuteNonQuery();
    UserWriteComment.Text = "";
}
Run Code Online (Sandbox Code Playgroud)

在Query中,有一个名为Date的值.这是我喜欢将当前日期传递到我的表中的函数.

我希望你能帮助我,因为我没有设法找到答案.

谢谢:)

c# sql datetime

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

将数据表搜索转换为 lambda 表达式

我需要帮助将此 for 循环转换为 VB.NET 中的 LINQ/Lambda 表达式:

For Each dr As DataRow In dt.Rows
    Dim firstName As String = CStr(dr("FirstName")).ToLower()
    Dim lastName As String = CStr(dr("LastName")).ToLower()

    If Not String.IsNullOrEmpty(firstName) AndAlso Not String.IsNullOrEmpty(lastName) Then
        If firstName.ToLower = "john" AndAlso lastName.ToLower = "doe" Then
            Return "Found"
        End If
    End If
Next

Return "Not Found"
Run Code Online (Sandbox Code Playgroud)

谢谢!

.net linq vb.net ado.net lambda

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

将字典细分为较小的部分

我试图将字典分成更小的部分.我的方法有效,我只是想知道是否有更有效的方法.为了这个例子,字典有10个元素,但是我的代码最多可以有1000个,并且是50个批次,我将它传递给api.

我的Questain是否有更有效的方法来做到这一点......?

int divideamount = 2;

Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("cat1", "cat");
dictionary.Add("dog2", "cat");
dictionary.Add("cat3", "cat");
dictionary.Add("dog4", "cat");
dictionary.Add("cat5", "cat");
dictionary.Add("dog6", "cat");
dictionary.Add("cat7", "cat");
dictionary.Add("dog8", "cat");
dictionary.Add("cat9", "cat");
dictionary.Add("dog10", "cat");


var divided_dictionary
    = dictionary.Select((value, index) => new { Index = index, Value = value })
                .GroupBy(x => x.Index / divideamount)
                .Select(g => g.Select(x => x.Value).ToDictionary(x => x.Key, x => x.Value))
                .ToList();


foreach (Dictionary<string, string> division in divided_dictionary)
{ 
    // do some work
}
Run Code Online (Sandbox Code Playgroud)

提前感谢您的任何帮助或评论.

c# linq dictionary

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

Linq 无法识别方法 GetDistanceTo

我有这个查询,我试图按距离排序。但是,linq 向我抛出一个错误,指出它无法识别 GetDistanceTo 方法。当删除 OrderBy 子句时,查询将起作用。

 var coord = new GeoCoordinate { Latitude = (double?)array.latitude ?? 0, Longitude = (double?)array.longitude ?? 0 };

 var property = db.Properties.Select(x => new SearchResultsViewModel
 {
      geocoord = new GeoCoordinate { Latitude = (double?)x.latitude ?? 0, Longitude = (double?)x.longitude ?? 0 }

 }).OrderBy(x=>x.geocoord.GetDistanceTo(coord)).ToList();
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework

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