【发布时间】:2015-09-19 23:12:28
【问题描述】:
鉴于下面的 edmx 文件,我正在尝试编写一个异步 ActionResult,它将为我提供任何用户角色尚未播放的所有场景的列表。我正在尝试找出最有效的方法以及将场景列表放到我的视图中的逻辑。我是 Linq 和 MVC 的新手,所以这很有挑战性。
我目前对 ActionResult 的尝试如下:
// GET: Home/ChooseScenario
public async Task<ActionResult> ChooseScenario(string charId)
{
//Convert passed in charId string to related Character object
var pFSCharacter = await db.PFSCharacters
.FirstOrDefaultAsync(c => c.PFSCHARACTERID == charId);
//Initialize empty list of Characters for the User
List<PFSCharacter> userCharacters = new List<PFSCharacter>();
//determine if the pFSCharacter is Core and only give me back a list of characters for the user that are core as well
if (pFSCharacter.IsCore)
{
userCharacters = await db.PFSCharacters
.Where(x => x.PFSUSERID == User.Identity.GetUserId() && x.IsCore == true)
.ToListAsync();
}
//If pFSCharacter is not core give me back a list of characters for the user that are not core
else
{
userCharacters = await db.PFSCharacters
.Where(x => x.PFSUSERID == User.Identity.GetUserId() && x.IsCore == false)
.ToListAsync();
}
List<PFSCharacterScenario> pFSCharacterScenarios = new List<PFSCharacterScenario>();
//try to figure out the list of Scenarios that none of the User's characters have already participated in
foreach (PFSCharacter character in userCharacters)
{
//can this be done better or in a different way that a foreach loop?
//can't figure out the logic to give back list of scenarios that each character has not played yet
//pFSScenarios = await db.PFSCharacterScenarios //this will overwrite the list need to add to the
//list already started for the other characters on
//previous loops
//.ToListAsync();
}
//need to remove duplicates scenarios from list so scenario only appears once.
//pair down list even further to scenarios that pFSCharacter.charLevel >= pFSScenario.minLevel
//and pFSCharacter.charLevel <= pFSScenario.maxLevel
//var pFSScenarios = db.PFSScenarios
// .Include(p => p.PFSFirstSubmittedBy)
// .Include(p => p.PFSScenarioType)
// .Where(p => p.IsActive == true)
// .OrderBy(p => p.PFSSCENARIOTYPEID)
// .ThenBy(p => p.Season)
// .ThenBy(p => p.SeasonID)
// .ThenBy(p => p.MinLevel)
// .ThenBy(p => p.ScenarioName);
//pass pFSCharacter to View to pass to next Controller ActionResult
ViewBag.Character = pFSCharacter;
//send list of scenarios to the View to be displayed.
return View(await pFSScenarios);
}
编辑:我和朋友一起制定了我需要使用的 SQL 查询。使用 LINQ 将其放入我的控制器的最佳方法是什么?
SELECT *
FROM PFSScenarios
Where PFSSCENARIOID NOT IN (
Select s.PFSSCENARIOID
FROM PFSScenarios s
Inner Join PFSCharacterScenarios cs
on s.PFSSCENARIOID = cs.PFSSCENARIOID
Inner Join PFSCharacters c
on c.PFSCHARACTERID = cs.PFSCHARACTERID
WHERE C.PFSUSERID = '35be8bbb-99dc-44bc-9d84-9bcc937d79d6'
)
EDIT3:接近,但仍然出错
Unable to create a constant value of type 'PFSScenarioTracker.Data.EF.PFSCharacter'. Only primitive types or enumeration types are supported in this context.
当使用下面的 Fabio Luz 解决方案时。
如果我使用空白 List<PFSCharacter> userCharacters = new List<PFSCharacter>(); 并注释掉填充 userCharacters 的 if/else statement,错误就会消失,但是它会返回数据库中不符合我添加的其他条件的每个场景 i => i.IsActive == true && i.MinLevel <= pFSCharacter.CharLevel && i.MaxLevel >= pFSCharacter.CharLevel &&。
这是我当前的控制器操作结果:
// GET: Home/ChooseScenario
public async Task<ActionResult> ChooseScenario(string charId)
{
string currentUser = User.Identity.GetUserId();
//Convert passed in charId string to related Character object
var pFSCharacter = await db.PFSCharacters
.FirstOrDefaultAsync(c => c.PFSCHARACTERID == charId);
List<PFSCharacter> userCharacters = new List<PFSCharacter>();
//List<PFSScenario> pFSScenarios = new List<PFSScenario>();
//determine if the pFSCharacter is Core and only give me back a list of characters for the user that are core as well
if (pFSCharacter.IsCore)
{
userCharacters = await db.PFSCharacters
.Where(x => x.PFSUSERID == currentUser && x.IsCore == true)
.ToListAsync();
}
//If pFSCharacter is not core give me back a list of characters for the user that are not core
else
{
userCharacters = await db.PFSCharacters
.Where(x => x.PFSUSERID == currentUser && x.IsCore == false)
.ToListAsync();
}
var pFSScenarios = await db.PFSScenarios.Where(i => i.IsActive == true && i.MinLevel <= pFSCharacter.CharLevel && i.MaxLevel >= pFSCharacter.CharLevel && !i.PFSCharacterScenarios.Any(x => userCharacters.Contains(x.PFSCharacter))).ToListAsync();
//pass pFSCharacter to View to pass to next Controller ActionResult
ViewBag.Character = pFSCharacter;
//send list of scenarios to the View to be displayed.
return View(pFSScenarios);
}
【问题讨论】:
标签: c# linq entity-framework asp.net-mvc-5