我有一些LINQ代码生成一个字符串列表,如下所示:
var data = from a in someOtherList
orderby a
select FunctionThatReturnsString(a);
Run Code Online (Sandbox Code Playgroud)
如何将该字符串列表转换为一个大的连接字符串?假设数据包含以下条目:
"Some "
"resulting "
"data here."
Run Code Online (Sandbox Code Playgroud)
我应该得到一个看起来像这样的字符串:
"Some resulting data here."
Run Code Online (Sandbox Code Playgroud)
我该怎么办?我想到了这个:
StringBuilder sb = new StringBuilder();
data.ToList().ForEach(s => sb.Append(s));
string result = sb.ToString();
Run Code Online (Sandbox Code Playgroud)
但这似乎并不合适.如果它是正确的解决方案,我将如何将其转换为扩展方法?
假设我有一组消息,其中包含"UserID"(int)和"Unread"(bool)属性.
对于UserID = 5的集合中的任何Message,如何使用LINQ扩展方法设置Unread = false?
所以,我知道我可以这样做:
messages.Any(m => m.UserID == 5);
Run Code Online (Sandbox Code Playgroud)
但是,如何使用扩展方法设置每个的Unread属性呢?
注意:我知道我不应该在生产代码中这样做.我只是想学习更多LINQ-fu.
这应该很简单,但我是LINQ的新手.我有一个List<FillStruct>的FillList结构.我想使用LINQ来创建一个新的List<NewFillStruct>,而不是购买和销售的数量,我将有一个包含总和的变量.
例如,如果FillStruct结构有
buy = 4
Run Code Online (Sandbox Code Playgroud)
和
sell = 2
Run Code Online (Sandbox Code Playgroud)
然后NewFillStruct结构将有
numlong = 2.
Run Code Online (Sandbox Code Playgroud)
如果FillStruct结构有
buy = 2
Run Code Online (Sandbox Code Playgroud)
和
sell = 4
Run Code Online (Sandbox Code Playgroud)
然后NewFillStruct结构将有
numlong = -2.
Run Code Online (Sandbox Code Playgroud)
这是结构.
struct FillStruct
{
int buy;
int sell;
string date;
}
struct NewFillStruct
{
int numlong;
string date;
}
Run Code Online (Sandbox Code Playgroud) 我有一个我必须使用的场景.选择LINQ中的位置.以下是我的查询.
List<DTFlight> testList = _ctrFlightList.Select(i => new DTFlight() { AirLineName = i.AirLineName,ArrivalDate = i.ArrivalDate }).ToList();
Run Code Online (Sandbox Code Playgroud)
我希望在这个查询中使用where(添加条件).
请帮忙......谢谢.
我正在使用LINQ to Objects,并想知道是否可以通过使用我拥有的索引来提高查询的性能.最好用一个例子来解释.想象一个简单的类型......
public class Person
{
public int Age;
public string FirstName;
public string LastName;
}
Run Code Online (Sandbox Code Playgroud)
一个简单的查询我会反对它...
List<Person> people = new List<Person>();
// 'people' populated with 50,000 instances...
var x = from t in people
where t.Age > 18 && t.Age < 21
select t;
Run Code Online (Sandbox Code Playgroud)
如果我正确理解LINQ to Objects,那么Where扩展方法的实现将枚举people集合中的所有50,000个实例,以便找到实际匹配的100个实例.碰巧我已经有一个按年龄排序的人员集合的索引.像这样...
SortedList<int, Person> ageSorted = new SortedList<int, Person>();
Run Code Online (Sandbox Code Playgroud)
很明显,如果我可以获得在哪里使用SortedList以便它不再需要枚举所有50,000个实例,而是找到100个匹配条目的范围,从而节省时间.
是否可以将LINQ扩展到Objects来实现我的情况?它已经可以但我错过了这项技术吗?
我有一个问题,我希望组类型强类型,但如果我这样做不正确分组.看下面的代码......
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication35
{
class Program
{
static void Main(string[] args)
{
List<Foo> foos = new List<Foo>();
foos.Add(new Foo() { Key = "Test" });
foos.Add(new Foo() { Key = "Test" });
foos.Add(new Foo() { Key = "Test" });
var groups = foos.GroupBy<Foo, dynamic>(entry => new
{
GroupKey = entry.Key
});
Console.WriteLine(groups.Count());
groups = foos.GroupBy<Foo, dynamic>(entry => new GroupingKey()
{
GroupKey = entry.Key
});
Console.WriteLine(groups.Count());
}
public class Foo
{
public string Key { get; …Run Code Online (Sandbox Code Playgroud) 我有2种不同类型的词典Dictionary<int, Fooclass> oldDic和Dictionary<int, string> barList newDic.现在我需要比较两个词典中的值.例如,密钥可以在
现在我需要根据他们的键来比较两个字典,任何帮助将不胜感激.
编辑:输出应该像第二个字典(newDic),但这将包含第二个字典(oldDic)的一些值.例如
1,"fooString"其中fooString是Fooclass的某些属性中的一些值....为了更清晰,请看这对我不起作用
var addedList = from list1 in baseListDic
join list2 in trackerlist on list1.Key equals list2.Key
select new { key = list1.Key, value = list1.Value._lead };
Run Code Online (Sandbox Code Playgroud)
这里baseListDic是oldDic,trackerlist是newDic ....如果我还不清楚,请告诉我...
我有这个问题困扰着我; 它被封装为一个新的查询运算符,我制作了两个版本,试图看看哪个更好.两者表现得非常糟糕.
第一次尝试; 陈述式
public static IEnumerable<IEnumerable<?>> Section<?>(this IEnumerable<?> source, int length)
{
return source.Any()
? source.Take(length).Cons(source.Skip(length).Section(length))
: Enumerable.Empty<IEnumerable<?>>();
}
Run Code Online (Sandbox Code Playgroud)
第二次尝试:势在必行的"收益率回报"风格
public static IEnumerable<IEnumerable<?>> Section<?>(this IEnumerable<?> source, int length)
{
var fst = source.Take(length);
var rst = source.Skip(length);
yield return fst;
if (rst.Any())
foreach (var section in rst.Section(length))
yield return section;
}
Run Code Online (Sandbox Code Playgroud)
事实上,第二次尝试在可读性,组合性和速度方面都更糟糕.
关于如何优化这个的任何线索?
另请参见LINQ to Objects和LINQ to SQL查询之间的差异
我们在数据库和内存对象中使用了一些查询.
与linq-to-sql相比,使用不敏感字符串的最佳方法是什么?
运用
a.ToLowerInvariant() == b.ToLowerInvariant()
Run Code Online (Sandbox Code Playgroud)
至少得到相同的结果,但据我所知,它不会在SQL服务器上处理,所以可能会慢得多
a == b
Run Code Online (Sandbox Code Playgroud) 我正在查询视图并使用名为status的列过滤结果.我想查询它,以便我可以通过使用IN运算符来搜索具有不同状态的行,就像我在SQL中所做的那样.
如此:
SELECT*FROM VIEW WHERE状态('....','.....')
我怎样才能做到这一点?
linq-to-objects ×10
c# ×8
linq ×8
.net ×3
c#-4.0 ×1
indexing ×1
linq-to-sql ×1
optimization ×1
performance ×1
string ×1