我可以Linq查询多个变量吗?

pro*_*don 3 c# sql linq

我对linq有点新意,我不确定我的愿望是否可行.我基本上有一个名为User的类,它包含一堆属性.我想只填写我从查询中获取的名称和ID字段.我的查询给了我适当的结果,但我不确定如何将其输入用户.这是我得到的最接近的,但我意识到这是不正确的.

 IEnumerable<User> UserList;
 UserList = (from o in dbContext.application_user
             join p in dbContext.project on application_user.user_id = project.project_manager
             select o);
Run Code Online (Sandbox Code Playgroud)

这将返回User_ID,Firstname和Lastname的列表,但它不适合用户.我的用户类有多个变量,所以我想通过调用3个IEnumerables来接近它,类型为int,string和string,如果我可以以某种方式填充一个查询中的所有3个,然后设置User = new User(name = x ,id = x)等

FNameList, LNameList, ID = *insert query here*
Run Code Online (Sandbox Code Playgroud)

Dav*_*vid 7

您可以User在Linq查询中构造对象:

IEnumerable<User> UserList;
UserList = (from o in dbContext.application_user
            join p in dbContext.project on application_user.user_id = project.project_manager
            select new User(o.FName, o.LName, o.ID));
Run Code Online (Sandbox Code Playgroud)

这假定User有一个构造函数取三个参数像,而且对性能oFName,LNameID,但你的想法.该select子句可用于将结果转换为您需要的任何格式.