我有以下查询,我正试图通过TCount获得TopicCounts的前4名:
var bygroup = (from element in inputSplit
group element by element.Text into groups
from win in groups.TumblingWindow(TimeSpan.FromSeconds(10))
select new TopicCounts
{
To
LINQ通过排序/转换数据结构进行优化吗?
即
LINQ是在这段代码中迭代,还是对数据进行任何类型的排序/转换以优化查找操作?
var list = new List<IdCard> {new IdCard(){Name = "a", Number = 1}, new IdCard() { Name = "b", Number = 2 } };
var idA = list.FirstOrDefault(id => id.Name == "a");
var idB = list.FirstOrDefault(id =>
我有一个一对多的Linq查询,我想对“多”集合中的一个属性进行排序。例如,在下面的伪代码中,从Linq查询返回了一个列表,但我希望根据Product类的SequenceNumber属性对Products属性进行排序。我该怎么做呢?感谢您提供的任何信息。谢谢。
public class Order
{
public int OrderId;
public List<Product> Products;
}
public class Product
{
public string name;
public int SequenceNumber;
}
我有一个通用的项目列表。每一项都包含一个DateTime字段。我想使用Linq以最优雅和高效的方式在列表中找到最新的项目。
在我的例子中,优雅比效率更重要,但以一种有效的方式做到这一点也会很好。
谢谢。
读完答案后:代码如下(也是我喜欢的答案):
using System.Collections.Generic;
using System.Linq;
class Item
{
public Item Date { get; set; }
public string Name { get; set; }
}
static void Main(string[] args)
{
我在我的项目(和LINQ)中使用Lucene从Product表中进行选择。这就是我为Lucene索引字段的方式:
var document = new Document();
document.Add(new Field("ID", HeaderSearchModel.ID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("Summary", HeaderSearchModel.Summary, Fie
我知道这可能是微优化,但我仍然想知道在使用
var lastObject = myList.OrderBy(item => item.Created).Last();
或
var lastObject = myList.OrderByDescending(item => item.Created).First();
我正在寻找Linq to objects和Linq to Entities的答案。
天气很热,所以我的大脑不太好用。用LINQ排序的最好方法是什么?注意,排序是基于"C“进行的,但适用于"M”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Compare
{
public class M
{
public IList<C> Columns = new List<C>();
}
public class C
{
publ
我有一个需要按Name排序的IQueryable<Product>。每个Product都有一个IQueryable<Category>,它也需要按Name排序。我很难用Linq来表达这一点。我可以遍历产品并对每个类别列表进行排序,但这看起来很混乱。希望Linq忍者能有更聪明的解决方案。
我的Product类如下所示:
public class Product {
public string Name { get; set; }
public IQueryable<Category> Categories { get; set; }
}
我的C
下面是我用C#编写的非常简单的linq查询
var latest = (from calc in context.db_accounts_last_contacts
group calc by calc.account_id into calcGroup
select calcGroup);
var ordered = (from calcg in latest
let item = (from items in calcg
orderby items.last_contact_date descending
select items)
我正在尝试按datetime字段进行排序,该字段包含以下对象的列表:
// Order search results (posts to be displayed by created datetime).
if (implicitSelectedVisualiser.PostsSortOrder == PostsSortOrder.CREATED_DATE_ASC)
approvedSearchResults.OrderBy(s => s.PostCreatedTime);
else
approvedSearchResults.OrderBy
我使用的是Windows8.1通用应用程序,我将我的共享代码放在共享项目中。
我有一个ViewModel,它需要调用LINQ方法来将属性重置为列表中的第一项,以下是代码。
public ObservableCollection<Movie> Movies { get; set; }
public Movie Current { get; set; }
...
public void Reset()
{
this.Current = this.Movies.FirstOrDefault();
}
但是,似乎LINQ在共享项目中不可用,并且我无法添加引用。
然而,我可以在Wi
我无法在我的可观察集合中显示我的查询。我以前从未使用过linq,我不知道该怎么做。我通过微软在上看了一个教程,但它甚至没有提供我想要做的事情。所有我需要的帮助是写代码来搜索整个数据库的ToDoItem和显示最高的整数值首先,然后按降序。下面是我的代码:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
var toDoItemsInDB = from ToDoItem todo in toDoDB.ToDoItems
我正在使用Linq To Nhibernate,通过HQL语句,我可以这样做:
string hql = "from Entity e order by rand()";
Andi t的排序是随机的,我想知道如何使用Linq to Nhibernate执行相同的语句?
我试试这个:
var result = from e in Session.Linq<Entity>
orderby new Random().Next(0,100)
select e;
但是它抛出了一个异常并且不能工作...
有没有其他方法或解
我有一个codefirst实体模型,它有两个实体,City和Locality。城市与地方有一对多的关系。下面是它们之间的关系。
public class City {
public virtual List<Locality> Localities { get; set; }
}
public class Locality {
public virtual City City { get; set; }
}
现在我有一些代码,可以找到一个特定的城市,并加载该城市的位置。我希望使用LINQ IQueryable按"Name“对加载的位置进行排序,如下所示
c
欢迎光临!我想使用LINQ来查询一个对象列表,并按照枚举值、字符串值对它们进行排序,然后合并列表的其余部分,在这里使用LINQ新手类似的方式进行排序,所以要小心。
//CTOR for my class
MyClass(id, "Name Value Here", Enum.EnumValueHere);
我创建了一个这些对象的列表,并希望对它们进行可变排序,这样所有带有Enum.EnumValueX的项都首先显示在列表中,按名称排序,然后是以类似方式排序的所有其他项(almnost类似于SQL中的结果集的联合)
//What I have so far (only sorts
我试图根据他们的时间戳来处理一些数据。我有一个可行的解决方案,但是我觉得使用LINQ可以用一种更干净的方式来表达它。
我需要做以下几点:
根据timestampRound上的millisecondsRemove对列表进行排序--任何重复的值(不管哪些值被删除)
以下是我目前的工作解决方案:
using MoreLinq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SortingWITS
我有一个PropertyDetails类
public class PropertyDetails
{
public int Sequence { get; set; }
public int Length { get; set; }
public string Type { get; set; }
}
我正在创建一个PropertyDetails列表,作为
List<PropertyDetails> propertyDetailsList=new List<PropertyDetails>();
我想按PropertyDetails
我的问题是基于提出的,我已经就这个问题发布了一个答案..
这是密码。
var lines = System.IO.File.ReadLines(@"C:\test.txt");
var Minimum = lines[0];//Default length set
var Maximum = "";
foreach (string line in lines)
{
if (Maximum.Length < line.Length)
{
Maximum = line;
}
if (Minimum.L
对我来说似乎没问题,但我的单子还是没有按我的钥匙订的。
var tagNamesAndRoutes = new Dictionary<string, string>();
foreach (string name in tagNames)
{
tagNamesAndRoutes.Add(name, routeConstant);
}
字典值示例:
Key Value
"NUnit" "/Category"
"WCF" "/Category"
我使用这个LINQ从泛型列表中获取特定类成员的不同值:
var distinctDescriptions = itemsForMonthYearList.Select(x => x.ItemDescription).Distinct();
通用列表是这样定义的:
List<ItemsForMonthYear> itemsForMonthYearList;
课程是:
public class ItemsForMonthYear
{
public String ItemDescription { get; set; }
public String monthYr