EFCore get a tables in a few fields

EFCore get a tables in a few fields

1. Background

Separating the rear end of the preceding application scenario, a sheet 20 has several fields tables, but may only use the front end of four fields, so that the field value related to the acquisition section in a table. This article describes three methods.

2. Method for a: linq

2.1 Select method

List<Emplayee> emplayeeList = GetEmplayeeList();            //获取员工信息列表
int[] empIds = emplayeeList.Select(a => a.Id).ToArray();    //获取员工编号数组

2.2 Use ForEach method

List<Emplayee> emplayeeList = GetEmplayeeList();  //获取员工信息列表
string empIds = "";
emplayeeList.ForEach(a => empIds += a.Id + ",");
empIds = empIds.TrimEnd(',');
Console.WriteLine(empIds);   //输出:1,2,3

2.3 Other reference code

/// <summary>  
/// 员工信息类  
/// </summary>  
public class Emplayee
{
    /// <summary>
    /// 编号
    /// </summary>
    public int Id { get; set; }
 
    /// <summary>  
    /// 姓名  
    /// </summary>  
    public string Name { get; set; }

/// <summary>  
/// 获取员工信息列表  
/// </summary>  
/// <returns></returns>  
public static List<Emplayee> GetEmplayeeList()
{
    List<Emplayee> emplayeeList = new List<Emplayee>();
    Emplayee emplayee1 = new Emplayee() { Id = 1, Name = "张三" };
    Emplayee emplayee2 = new Emplayee() { Id = 2, Name = "李四" };
    Emplayee emplayee3 = new Emplayee() { Id = 3, Name = "王五" };
    emplayeeList.Add(emplayee1);
    emplayeeList.Add(emplayee2);
    emplayeeList.Add(emplayee3);
    return emplayeeList;
}

3. Act II: IQueryble

A reference example 3.1

I have a news list

id,title,body,createtime,author,click

Use ef4.1 read only id, title, createtime and displayed on the page.

 public static List<NewInfo> GetHotNews()
        {
            List<NewInfo> list;
            list = (from n in db.NewInfoes
                    where n.IsTop == 1
                    orderby n.PublishTime descending
                    select new
                    {
                        Title = n.Title,
                        NewID = n.NewID,
                        PublishTime = n.PublishTime
                    }).ToList();
            return list;
        }
改为
public static List<NewInfo> GetHotNews()
        {
            List<NewInfo> list;
            list = (from n in db.NewInfoes
                    where n.IsTop == 1
                    orderby n.PublishTime descending
                   
       .Select(n => new NewInfo
                    {
                        Title = n.Title,
                        NewID = n.NewID,
                        PublishTime = n.PublishTime
                    });
            return list;
        }

Note: just IEnumerable is to pull all the data memory screening, IQueryable will form a sql query directly in the library return

3.2 Reference example two

4. Act III: create an object, and then configure a automapper, the query results are then matched up

Specific can try on their own

Reference:
1. Sign Kawhi (1585955375), Judith Altman Troy Ostrovsky (479 663 032)
2.https: //blog.csdn.net/lcnmdfx/article/details/8332401
3.https: // Blog. csdn.net/pan_junbiao/article/details/51757904

Guess you like

Origin www.cnblogs.com/JerryMouseLi/p/11720244.html