返回IQueryable<T>与IEnumerable<T>?有什么区别?
IQueryable<Customer> custs = from c in db.Customers
where c.City == "<City>"
select c;
IEnumerable<Customer> custs = from c in db.Customers
where c.City == "<City>"
select c;
Run Code Online (Sandbox Code Playgroud)
两者都会延迟执行,何时应该优先于另一个?
我经常遇到我想在我声明它的地方评估查询的情况.这通常是因为我需要多次迭代它并且计算起来很昂贵.例如:
string raw = "...";
var lines = (from l in raw.Split('\n')
let ll = l.Trim()
where !string.IsNullOrEmpty(ll)
select ll).ToList();
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,如果我不打算修改结果,那么我不妨打电话给ToArray()而不是ToList().
我想知道是否ToArray()通过第一次调用实现,ToList()因此内存效率低于仅调用ToList().
我疯了吗?我应该只是打电话ToArray()- 安全且安全地知道内存不会被分配两次吗?
其中一个扩展方法IEnumerable<T>是.AsEnumerable().此方法将其调用的可枚举对象转换为实例IEnumerable<T>.但是,由于对象必须实现IEnumerable<T>才能应用于此扩展方法,因此转换为IEnumerable<T>简单的转换方法IEnumerable<T>.我的问题是为什么这种方法存在?
例:
List<string> strings = new List<string>() { "test", "test2", "test3" };
IEnumerable<string> stringsEnum1 = strings.AsEnumerable();
IEnumerable<string> stringsEnum2 = (IEnumerable<string>)strings;
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,stringsEnum1并且stringsEnum2是等价的.扩展方法有什么意义?
编辑:作为推论,为什么.AsQueryable()在施法时有一种方法IQueryable<T>是等价的?
在MVC3 Web应用程序中获取错误.
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
当我尝试从查询中使用EF获取值时:
public class DataRepository
{
public mydataEntities1 dbContext = new mydataEntities1();
public List<SelectListItem> GetPricingSecurityID()
{
var pricingSecurityID = (from m in dbContext.Reporting_DailyNAV_Pricing
select new SelectListItem
{
Text = m.PricingSecurityID.ToString(),
Value = m.PricingSecurityID.ToString()
});
return pricingSecurityID.ToList();
}
}
Run Code Online (Sandbox Code Playgroud) 在这里和这里阅读问题让我对这种情况有所了解,似乎使用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) 我有一个方法,返回一组帐户
Public Shared Function GetAllNotesByUser(ByVal UserID As Guid) As Account (??)
Using db As New MyEntity
Dim query= (From A In db.Account _
Where A.aspnet_Users.UserId = UserID _
Select A)
Return query
End Using
End Function
Run Code Online (Sandbox Code Playgroud)
然后,我想将此传递给另一个函数来计算集合中所有帐户的总计.最好的做法是返回一个Ienumerable,一个通用列表,我只是不确定什么最适合LINQ和实体框架.
linq ×7
c# ×4
.net ×2
entity ×2
ienumerable ×2
asp.net-mvc ×1
asqueryable ×1
enumerable ×1
iqueryable ×1
linq-to-sql ×1
list ×1
performance ×1
vb.net ×1