我对枚举器如何工作以及LINQ有些怀疑.考虑这两个简单的选择:
List<Animal> sel = (from animal in Animals
join race in Species
on animal.SpeciesKey equals race.SpeciesKey
select animal).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
要么
IEnumerable<Animal> sel = (from animal in Animals
join race in Species
on animal.SpeciesKey equals race.SpeciesKey
select animal).Distinct();
Run Code Online (Sandbox Code Playgroud)
我更改了原始对象的名称,因此这看起来像一个更通用的示例.查询本身并不重要.我想问的是:
foreach (Animal animal in sel) { /*do stuff*/ }
Run Code Online (Sandbox Code Playgroud)
我注意到,如果我使用IEnumerable
,当我调试并检查"sel"时,在这种情况下是IEnumerable,它有一些有趣的成员:"inner","outer","innerKeySelector"和"outerKeySelector",这些最后2个出现成为代表."内部"成员中没有"Animal"实例,而是"Species"实例,这对我来说非常奇怪."外部"成员确实包含"Animal"实例.我假设两位代表确定哪些进入,哪些进出?
我注意到如果我使用"Distinct","inner"包含6个项目(这是不正确的,因为只有2个是Distinct),但"outer"确实包含正确的值.同样,委托方法可能决定了这一点,但这比我对IEnumerable的了解要多一些.
最重要的是,两种选择中的哪一种是性能最佳的?
邪恶的名单转换通过.ToList()
?
或者直接使用枚举器?
如果可以,请解释一下或抛出一些解释IEnumerable使用的链接.
如何使用lambda将多列分组?
我看到了如何使用linq实现它的示例,但我正在寻找lambda形式.
我怎么能转换List<MyObject>
到IEnumerable<MyObject>
,然后再回来?
我想这样做是为了在List上运行一系列LINQ语句,例如 Sort()
我在记住如何(但不是为什么)IEnumerator
在C#中使用s 时遇到了麻烦.我已经习惯了Java的精彩文档,可以很好地向初学者解释一切.所以拜托,请耐心等待.
我试过从这些板上的其他答案中学习无济于事.我没有问过之前已经提出的一般性问题,而是有一个具体的例子可以为我澄清一些事情.
假设我有一个需要传递IEnumerable<String>
对象的方法.所有方法需要做的是将字母连接roxxors
到String
迭代器中每个字符的末尾.然后它将返回这个新的迭代器(当然原始IEnumerable
对象保持不变).
我该怎么做?当然,除了我之外,这里的答案应该可以帮助许多人解决有关这些对象的基本问题.
在这里和这里阅读问题让我对这种情况有所了解,似乎使用AsEnumerable是内存消耗.有没有更好的方法来实现这个LINQ以及现在的方式,数据是否可靠?
删除AsEnumerable导致"除了Contains运算符之外,本地序列不能用于查询运算符的LINQ to SQL实现".
var results = from p in pollcards.AsEnumerable()
join s in spoils.AsEnumerable() on new { Ocr = p.OCR, fileName = p.PrintFilename } equals new { Ocr = s.seq, fileName = s.inputFileName }
where p.Version == null
orderby s.fileOrdering, s.seq
select new ReportSpoilsEntity
{
seq = s.seq,
fileOrdering = s.fileOrdering,
inputFileName = s.inputFileName,
Ocr = p.OCR,
ElectorName = p.ElectorName
};
Run Code Online (Sandbox Code Playgroud) 示例代码:
List<Student> Students = new List<Student>()
{
new Student(101, "Hugo", "Garcia", new List<int>() { 91, 88, 76, 93 }),
new Student(102, "Rick", "Adams", new List<int>() { 70, 73, 66, 90 }),
new Student(103, "Michael", "Tucker", new List<int>() { 73, 80, 75, 88 }),
new Student(104, "Fadi", "Fakhouri", new List<int>() { 82, 75, 66, 84 }),
new Student(105, "Peter", "Barrows", new List<int>() { 67, 78, 70, 82 })
};
var query = from student in Students
where student.Marks.AsQueryable().All(m => m > …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将(Entity Framework 6)实体序列化为json.通过AsNoTracking()方法序列化之前我确保条目在内存中但是我得到一个错误,因为它无法从条目中引用的另一个表接收值.
Inner Exception: When an object is returned with a NoTracking merge option, Load can only be called when the EntityCollection or EntityReference does not contain objects.
Exception: JsonSerializationException: Error getting value from 'TABLE_X' on 'System.Data.Entity.DynamicProxies....
Run Code Online (Sandbox Code Playgroud)
码:
List<Location> locations = new DbContext().Locations.Where(x => x.Type == 1).Take(5).AsNoTracking().ToList();
string s = JsonConvert.SerializeObject(locations, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
Run Code Online (Sandbox Code Playgroud)
我想要做的就是返回一个序列化实体的字符串.我不担心其他对象,仅仅是位置实体.
当我尝试处理连接然后json序列化我收到错误: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
我只想序列化我的列表,我不想返回/序列化任何外来依赖项.
鉴于此代码段可以轻松粘贴到Linqpad中(或在Visual Studio控制台解决方案中稍作修改):
void Main()
{
var cat = this.GetCat();
var dog = this.GetDog();
cat.Think();
cat.ThinkHarder();
//dog.Think(); // Does not compile.
//dog.ThinkHarder(); // Does not compile.
//if ([dog is returned as ISmartAnimal]) // What to put here?
((ISmartAnimal)dog).Think(); // Compiles, runs, but shouldn't.
reportTypeProperties(cat);
reportTypeProperties(dog);
}
interface IAnimal
{
string Name { get; set; }
}
interface ISmartAnimal : IAnimal
{
void Think();
}
class Animal : IAnimal, ISmartAnimal
{
public string Name { get; set; }
public void Think() { …
Run Code Online (Sandbox Code Playgroud) 我需要从一个需要在LINQ查询中运行的函数中获取结果.这个结果绑定到网格但在运行时遇到这个错误:
LINQ to Entities无法识别方法'System.String GetName(System.Type,System.Object)'方法,并且此方法无法转换为商店表达式.
这是我的代码:
public IQueryable GetForRah_CapacityList(XQueryParam param)
{
var result = (from x in Data()
select new
{
Rah_CapacityId = x.Rah_CapacityId,
Rah_CapacityName = x.Rah_CapacityName,
Rah_St = Enum.GetName(typeof(Domain.Enums.CapacityState), x.Rah_St),
Rah_LinesId = x.Rah_LinesId
}).OrderByDescending(o => new { o.Rah_CapacityId });
return result;
}
Run Code Online (Sandbox Code Playgroud) 虽然有必要从一个LINQ驱动程序切换到另一个LINQ驱动程序(例如,因为某些想要的表达式在第一个中不受支持)应该使用什么样的开关 - AsEnumerable
或者ToList
?
var r = ent.Users.Select(user => new
{
user.Name,
Organs = user.Attributes.Select(x => x.Organ)
})
.AsEnumerable() // switch to LINQ to Objects
.Select(user => new
{
user.Name,
Organs = string.Join(", ", user.Organs)
});
Run Code Online (Sandbox Code Playgroud)
我明白这AsEnumerable
是延迟的,所以它不会立即枚举来源,但是ToList
在实践中,作为开关的用法是否有任何显着差异?
对于要在此处运行的LINQ to Objects,来自SQL的数据应该已经可用 - 因此ToList()
如果在交换机的位置使用它将会执行的操作.在评估表达式树的时候,是不是要在内部调用AsEnumerable
强制的使用ToList
?
EF在AsEnumerable()
使用时下载所有结果行的解释是什么?
我的意思是这段代码:
context.Logs.AsEnumerable().Where(x => x.Id % 2 == 0).Take(100).ToList();
Run Code Online (Sandbox Code Playgroud)
将任何行传递给Where()
方法之前,将从表中下载所有行,并且表中可能有数百万行.
我想要它做的是,只下载足以收集满足Id % 2 == 0
条件的100行(最可能只有大约200行).
EF无法按需使用普通的ADO.NET使用Read()
方法加载行,SqlDataReader
并节省时间和带宽吗?
我认为它出于某种原因不能正常工作,我希望听到一个支持该设计决策的好论据.
注意:这是一个完全人为的例子,我通常知道你不应该这样使用EF,但我在一些现有的代码中发现了这一点,并且我的假设被证明是不正确的.
c# ×11
linq ×8
list ×3
.net ×2
ienumerable ×2
asqueryable ×1
entity ×1
enumerable ×1
ienumerator ×1
interface ×1
iqueryable ×1
lambda ×1
reflection ×1
repository ×1