TLDR:我正在尝试将用户A 的参考显示为用户B ,我希望用户B 能够识别来自用户A 的参考。
在我的应用程序中,我有一个部分,我将用户相互连接以查看他们的关系(例如兄弟,姐妹等)。此部分称为案例交叉参考。< / p>
我使用案例GUID 连接用户。
实施例:
如果我想将用户A 连接到用户B ,则只需将我连接的用户设为 From_Case 另一个用户为 To_Case 。
现状 :
我目前能够显示从用户A 到用户B 的连接,反之亦然但是当我转到用户B ,它没有显示正确的案例编号或关系。
ToCases = await db.CaseCrossReference
.Include(x => x.Relationship)
.Include(x => x.Type)
.Include(x => x.Case2.CaseNumber)
.Where(x => (x.From_Case == cid || x.To_Case == cid)
&& x.Deleted == false)
.Select(o => new CaseCrossReferenceToRelationshipViewModel()
{
CaseIdFrom = o.From_Case,
CaseIdTo = o.To_Case,
CaseCrossReferenceId = o.CaseCrossReferenceId,
CaseNumber = o.Case2.CaseNumber == null ? null : o.Case2.CaseNumber.CaseNumberPrefix + "-" + o.Case2.CaseNumber.Number,
LivingTogether = o.LivingTogether,
Split = o.Split,
SplitDate = o.SplitDate,
SplitReason = o.SplitReason,
Type = o.Type.Definition,
RelationshipDefinition = o.Relationship.Definition
}).ToListAsync()
foreach (var cases in model.ToCases)
{
// Check for any reverse relationships
var reference = await db.CaseCrossReference.Include(x => x.Relationship).Include(x => x.Case1.CaseNumber).FirstOrDefaultAsync(m => m.From_Case == cases.CaseIdFrom && m.To_Case == cases.CaseIdTo);
if (reference != null)
{
if (cases.Gender == "M")
{
cases.RelationshipDefinition = await relationshipsdd.Where(r => r.Id == reference.Relationship.Id).Select(r => r.MaleReverseRelationshipDefinition).FirstOrDefaultAsync();
cases.CaseCrossReferenceId = reference.CaseCrossReferenceId;
cases.CaseNumber = reference.Case1.CaseNumber?.Number == null ? null : reference.Case1.CaseNumber.CaseNumberPrefix + "-" + reference.Case1.CaseNumber.Number;
}
else
{
cases.RelationshipDefinition = await relationshipsdd.Where(r => r.Id == reference.Relationship.Id).Select(r => r.FemaleReverseRelationshipDefinition).FirstOrDefaultAsync();
cases.CaseCrossReferenceId = reference.CaseCrossReferenceId;
cases.CaseNumber = reference.Case1.CaseNumber?.Number == null ? null : reference.Case1.CaseNumber.CaseNumberPrefix + "-" + reference.Case1.CaseNumber.Number;
}
}
}
您可能想知道什么是 cid 。它是 CaseID / Case GUID 查询字符串值。当我在用户A 时,cid是用户A guid,当我转到用户B 时,它就是用户A 用户B guid。只需它是当前的案例ID。
我需要能够执行以下操作:
无论我在哪个用户中,我都应该能够看到与其他用户的连接。如果我在用户B 中,那么我应该能够看到我与用户A 有连接,反之亦然。
下面的图片展示了我在 PS-100002 中的正确情况,我可以看到我已连接到 PS-100000 。
但如果转到 PS-100000 ,我仍然会看到 PS-100000 作为我的参考。它需要 PS-100002 与正确的关系(祖父)。
你能帮我找一个合适的解决方案吗?
此主题的专用空间:https://chat.stackexchange.com/rooms/60854/room-for-forrest-and-kevin-maxwell
答案 0 :(得分:2)
我基本上不得不扭转成员之间的关系。所以我提出了以下解决方案:
var member = await db.Member.Include(x => x.Gender).Include(x => x.Case.CaseNumber)
.FirstOrDefaultAsync(x => x.Relationship.Code == "PA" && x.CaseId == cid && x.Deleted == false);
var relationshipsdd = db.RelationshipDD;
var model = new CaseCrossReferenceFromRelationshipViewModel()
{
MemberId = member?.MemberId,
FirstName = member?.FirstName,
MiddleName = member?.MiddleName,
LastName = member?.LastName,
Gender = member?.Gender.Code,
CaseNumber = member?.Case.CaseNumber?.Number == null
? null
: member.Case.CaseNumber?.CaseNumberPrefix + "-" + member.Case.CaseNumber?.Number,
CaseId = member?.CaseId,
ToCases = await db.CaseCrossReference
.Where(x => (x.From_Case == cid || x.To_Case == cid)
&& x.Deleted == false)
.Select(o => new CaseCrossReferenceToRelationshipViewModel()
{
CaseIdFrom = o.From_Case,
CaseIdTo = o.To_Case,
CaseCrossReferenceId = o.CaseCrossReferenceId,
LivingTogether = o.LivingTogether,
Gender = o.Relationship.Gender.Code,
Split = o.Split,
SplitDate = o.SplitDate,
SplitReason = o.SplitReason,
Type = o.Type.Definition,
RelationshipDefinition = o.Relationship.Definition,
Relationship = o.Relationship.Id
}).ToListAsync()
};
foreach (var cases in model.ToCases)
{
// Check for any reverse relationships/Case Number
var reference = await db.CaseCrossReference.Include(x => x.Relationship)
.Include(x => x.Case2.CaseNumber).Include(x => x.Case1.CaseNumber)
.FirstOrDefaultAsync(m => m.From_Case == cases.CaseIdFrom && m.To_Case == cases.CaseIdTo);
if (cases.CaseIdFrom == cid)
{
member = await db.Member.Include(x => x.Gender)
.FirstOrDefaultAsync(x => x.Relationship.Code == "PA" && x.CaseId == cases.CaseIdTo &&
x.Deleted == false);
cases.CaseIdFrom = cases.CaseIdTo;
cases.CaseNumber = reference.Case2.CaseNumber?.Number == null
? null
: reference.Case2.CaseNumber.CaseNumberPrefix + "-" + reference.Case2.CaseNumber.Number;
cases.FirstName = member.FirstName;
cases.MiddleName = member.MiddleName;
cases.LastName = member.LastName;
if (member.Gender.Code == "M")
{
cases.RelationshipDefinition = await relationshipsdd
.Where(r => r.Id == reference.Relationship.Id)
.Select(r => r.MaleReverseRelationshipDefinition).FirstOrDefaultAsync();
}
else
{
cases.RelationshipDefinition = await relationshipsdd
.Where(r => r.Id == reference.Relationship.Id)
.Select(r => r.FemaleReverseRelationshipDefinition).FirstOrDefaultAsync();
}
}
else
{
member = await db.Member.Include(x => x.Gender)
.FirstOrDefaultAsync(x => x.Relationship.Code == "PA" && x.CaseId == cases.CaseIdFrom &&
x.Deleted == false);
cases.CaseIdTo = cases.CaseIdFrom;
cases.CaseNumber = reference.Case1.CaseNumber?.Number == null
? null
: reference.Case1.CaseNumber.CaseNumberPrefix + "-" + reference.Case1.CaseNumber.Number;
cases.FirstName = member.FirstName;
cases.MiddleName = member.MiddleName;
cases.LastName = member.LastName;
}
}
return View(model);
答案 1 :(得分:1)
只是不要.ToList()第一个查询。使用ToList()获取结果后,您必须使用延迟加载来导航导航属性。但是如果它在一个查询中,则将在单个SQL查询中提取用于投影ViewModel类型的列。
ToCases = await db.CaseCrossReference
.Include(x => x.Relationship)
.Include(x => x.Type)
.Where(x => (x.From_Case == cid || x.To_Case == cid)
&& x.Deleted == false)
.Select(o => new CaseCrossReferenceToRelationshipViewModel()
{
CaseId = o.From_Case,
CaseCrossReferenceId = o.CaseCrossReferenceId,
CaseNumber = o.Case2.CaseNumber?.Number == null ? null : o.Case2.CaseNumber.CaseNumberPrefix + "-" + o.Case2.CaseNumber.Number,
LivingTogether = o.LivingTogether,
Split = o.Split,
SplitDate = o.SplitDate,
SplitReason = o.SplitReason,
Type = o.Type.Definition,
RelationshipDefinition = o.Relationship.Definition
}).ToListAsync();