Tag: linq

如何使用Linq反序列化xml?

如何使用Linq反序列化这个xml? 我想创建List 1 Step 1 Step 1 Description 2 Step 2 Step 2 Description 3 Step 3 Step 3 Description 4 Step 4 Step 4 Description

EF非静态方法需要目标

我对以下查询存在严重问题。 context.CharacteristicMeasures .FirstOrDefault(cm => cm.Charge == null && cm.Characteristic != null && cm.Characteristic.Id == c.Id && cm.Line != null && cm.Line.Id == newLine.Id && cm.ShiftIndex != null && cm.ShiftIndex.Id == actShiftIndex.Id && (newAreaItem == null || (cm.AreaItem != null && cm.AreaItem.Id == newAreaItem.Id))); 我得到一个TargetException: Non-static method requires a target当newAreaItem为null时, TargetException: Non-static method requires a target […]

LINQ中的Contains和Any有什么区别?

LINQ中的Contains和Any什么区别?

如何使用entity framework按日期分组,而不是按时间分组

mycode的: //get data var myData = from log in db.OperationLogs group log by log.CreateTime.Date into g orderby g.Key select new { CreateTime = g.Key, Count = g.Count() }; 这段代码会抛出一个exception,就像entity framework不支持get Date操作一样。 因为log.createtime都有日期和时间,我想按日期分组,我该怎么做

将List 隐式转换为List

我正在使用Linq to Entities。 有一个实体“Order”,它有一个可为空的列“SplOrderID”。 我查询我的订单列表 List lst = Orders.where(u=> u.SplOrderID != null).Select(u => u.SplOrderID); 我理解这是因为SplOrderID是一个可以为空的列,因此select方法返回nullable int。 我只是期待LINQ有点聪明。 我该怎么处理?

如何动态构建entity framework查询?

我对Entity Framework很新,我对过滤数据有疑问。 我有两个不同的Log实体,它们是: DiskLog和NetworkLog 。 这些实体都是从Log实体派生的。 以下是我的C#应用​​程序中的一些代码: public class Log { … } public class DiskLog : Log { … } public class NetworkLog : Log { … } public enum LogType { NotInitialized = 0, Disk, Network } public List GetWithFilter( Guid userKey, int nSkip, int nTake, DateTime dateFrom = DateTime.MinValue, DateTime dateTo = […]

Linq To Sql仅比较时间

如何在不获取以下内容的情况下仅比较DateTime对象的时间 错误: An exception of type ‘System.NotSupportedException’ occurred in mscorlib.dll but was not handled in user code Additional information: The specified type member ‘TimeOfDay’ is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. 我的代码: var date = DateTime.Parse(query.DueTime); entities = entities.Where(r => r.DueTime.TimeOfDay.Equals(date.TimeOfDay));

Linq通过变量访问属性

假设我有一个类: public class Foo { public string Title {get;set;} } 现在,让我们假设我有一个public List myList ,我希望Linq过滤它: var x = myList.Where(f => f.Title == myValue); 到目前为止,一切都很美好。 但是如何通过变量访问属性? 就像是: string myProperty = “Title”; var x = myList.Where(f => f.myProperty == myValue);

LINQ:具有多个条件的单个位置与具有单个条件的连续Wheres之间的差异

在LINQ中连接多个Where而不是使用具有多个条件的单个Where有什么缺点吗? 我问,因为使用多个Where可以帮助降低复杂性并大大提高代码的可维护性。 考虑以下代码, chargeList是List ,它是BindingSource的源: IEnumerable matchingCharges = chargeList; if(!string.IsNullOrWhiteSpace(channelAbbr)) matchingCharges = matchingCharges .Where(c => c.ChannelAbbreviation == channelAbbr); if(deliveryNoteDate.HasValue) matchingCharges = matchingCharges .Where(c => c.ArrivalAt == deliveryNoteDate.Value); if(chargeID.HasValue) matchingCharges = matchingCharges .Where(c => c.ChargeID == chargeID.Value); 这个简洁的代码将处理filter,none,one,two,all的所有组合。 否则我必须在一个Where使用if-else和多个条件。 这是我脑海中最好的: // important to keep code readable: bool filterChannel = !string.IsNullOrWhiteSpace(channelAbbr); bool filterDate = deliveryNoteDate.HasValue; bool filterID […]

如何使用Reflections将DataTable转换为List

我有一个类的通用列表,我使用Reflection和扩展方法自动将其转换为DataTable。现在我想反向进行。我想将DataTable转换为List.Better说我想要帮助写一个方法期望一个DataTable和一个Type并根据列名自动查找该类型(类)的属性,并为该Type(类)的对象赋值。就像这个psodu代码: private List ConvertToList(DataTable dt) { List AllColumns = // Get All Column Names of a DataTable for(int i=0;i<dt.Rows.Count;i++) { foreach(var item in AllColumns ) { //Get Property According To **ITEM** //Get Data Of Rows[i][item] and assign it to T.property } } } 我怎么能这样做? 编辑1) 我用@Cuong Le的回答是这样的: var properties = typeof(CustomType).GetProperties().ToList(); List list = ConvertToList(dt, […]