IQueryable通过LastOrNull方法扩展 [英] IQueryable extend by method LastOrNull

查看:89
本文介绍了IQueryable通过LastOrNull方法扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求紧急协助书写方法的好日子,因为表达式为
需要获得IQueryable的最后记录

手动选项所有作品,在表达式中; 无法完成该方法。



请帮忙,这确实会停滞不前!

公共类用户
{
public int Id {get; set;}
public string Name {get; set;}
}

public列表<使用者> GetDemoData()
{
返回新列表<用户>
{
new User(){Id = 1,Name =" Alex"},
new User(){Id = 2,Name =" Betty"},
new User(){Id = 3,Name =" Gannet"}
};
}

公共静态类IQueryableEx
{
公共静态TSource LastOrNull< TSource>(
此IQueryable< TSource>源,
表达式< ; Func< TSource,object>> f)
其中TSource:class
{
var max = source.Max(f);
TSource lastItem = null;

//无法完成
// lastItem = source.Single(????????????);

返回lastItem;
}
}

void Main()
{
var data = GetDemoData()。AsQueryable();

//返回最后一个用户Gannet
var max = data.Max(u => u.Id);
var lastUser = data.Single(u => u.Id == max); // WorkFine
//lastUser.Dump();

//通过分机返回最后一个用户Gannet
//未结束编码
lastUser = data.LastOrNull(u => u.Id); //无法完成方法。
// lastUser.Dump();
}


请帮忙!

解决方案


http://msdn.microsoft.com/en-us/library/bb504095.aspx  (LastOrDefault)没有'是否符合您的需求?




Good day to ask for urgent assistance in writing a method with Expression
need to get the last record of IQueryable
manual option all works, in Expression  can `t finish the method.

Please help and that does come to a standstill!

public class User
{
  public int Id {get;set;}
  public string Name {get;set;}
}

public List<User> GetDemoData()
{
   return new List<User>
	{
		new User(){Id=1, Name="Alex"},
		new User(){Id=2, Name="Betty"},
		new User(){Id=3, Name="Gannet"}
	};
}

public static class IQueryableEx
    {
        public static TSource LastOrNull<TSource>(
            this IQueryable<TSource> source,
            Expression<Func<TSource, object>> f)
            where TSource:class            
        {
            var max = source.Max(f);
            TSource lastItem=null;
			
			//can `t finish the 
			//lastItem = source.Single(????????????);
			
	        return lastItem;
        }
	}

void Main()
{
    var data = GetDemoData().AsQueryable();
	
	// Return last User Gannet
	var max=data.Max(u=>u.Id);
	var lastUser=data.Single(u=>u.Id==max); // WorkFine
	//lastUser.Dump();	
	
	// Return last User Gannet by extension	
	// not end coded
	lastUser=data.LastOrNull(u=>u.Id); // can `t finish the method. 	
	// lastUser.Dump();	
}

Please help!

解决方案

Hi,

http://msdn.microsoft.com/en-us/library/bb504095.aspx (LastOrDefault) doesn't fit your needs ?


这篇关于IQueryable通过LastOrNull方法扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆