也许我的眼睛因为我整天都在编码而交叉,但我决定使用linq表达式并且它不会在关键字上编译...请注意这在linqPad中有效。
using System.Linq;
public IQueryable<CompanyPerson> PersonsFlattened()
{
var contacts = from person in Person
join companyPerson in CompanyPerson on person.Id equals companyPerson.PersonId into companyPersonGroups
from companyPerson in companyPersonGroups.DefaultIfEmpty()
select new
{
ContactPerson = person,
ContactCompany = companyPerson.Company
};
}
出于某种原因&#34;加入&#34;在第2行给我一个错误
&#34;无法解析符号&#39; GroupJoin&#39;&#34;
我甚至尝试过另一种方式。
var contacts2 = Person.GroupJoin(CompanyPerson,
person => person.Id,
companyPerson => companyPerson.PersonId,
(person, companyPersonGroups) =>
new
{
person = person,
companyPersonGroups = companyPersonGroups
}
).SelectMany(
temp0 => temp0.companyPersonGroups.DefaultIfEmpty(),
(temp0, companyPerson) =>
new
{
ContactPerson = temp0.person,
ContactCompany = companyPerson.Company
}
);
我得到同样的事情,有什么想法吗?我错过了一些你必须要表达的参考吗?我遍历了对象浏览器,但没有看到我没有包含的任何东西。它是一个web api 2项目.net 4.5
答案 0 :(得分:0)
这是因为您提供了类型CompanyPerson
而非第一个参数的值:
var contacts2 = Person.GroupJoin(companyPerson,
person => person.Id,
companyPerson => companyPerson.PersonId,
(person, companyPersonGroups) =>
new
{
person = person,
companyPersonGroups = companyPersonGroups
}
).SelectMany(
temp0 => temp0.companyPersonGroups.DefaultIfEmpty(),
(temp0, companyPerson) =>
new
{
ContactPerson = temp0.person,
ContactCompany = companyPerson.Company
}
);
答案 1 :(得分:0)
弄清楚GroupJoin编译问题,由于某种原因,它无法使用var contact = xxx正确编译
我必须返回一个QueryAll(Person)
return _contextProvider.QueryAll<Person>().GroupJoin(...)...
这是最终的解决方案。左sql是否加入并获取人员,即使他们没有与他们相关联的公司。
[HttpGet]
public IQueryable<Person> PersonsFlattened()
{
//return _contextProvider.QueryAll<Person>();
var contacts = from person in _contextProvider.QueryAll<Person>()
join companyPerson in CompanyPersons() on person.Id equals companyPerson.PersonId into companyPersonGroups
from companyPerson in companyPersonGroups.DefaultIfEmpty()
select new Person()
{
FirstName = person.FirstName,
LastName = person.LastName
};
return contacts;
}