Sono stato bloccato per un bel po 'questa sera cercando di unirmi ai tavoli. Ho delle Challenges
tavolo che hanno la categoria Id e voglio unirmi alla tabella delle Category
per ottenere il titolo attuale invece di dover fare l'indicizzazione in frontend che sto facendo ora.
public List<Challenges> GetChallenges()
{
var list = new List<Challenges>();
var queryable = list.AsQueryable();
var challengesQ = (from ch in challengeContext.Challenges
join cc in challengeContext.ChallengeCategories
on ch.Category equals cc.Id
select new {
id = ch.Id,
title = ch.Title,
createdAt = ch.CreatedAt,
daysNeeded = ch.DaysNeeded,
reward = ch.Reward,
difficulty = ch.Difficulty,
completedBy = ch.CompletedBy,
imgUrl = ch.ImgUrl,
subcategory = cc.Title,
category = cc.Title,
instructions = ch.Instructions
}
);
DbSet<Challenges> challenges = challengeContext.Challenges;
DbSet<ChallengeCategories> categories = challengeContext.ChallengeCategories;
var q = challenges.Join(
categories,
ch => ch.Category,
cc => cc.Id,
(ch, cc) => new
{
id = ch.Id,
title = ch.Title,
createdAt = ch.CreatedAt,
daysNeeded = ch.DaysNeeded,
reward = ch.Reward,
difficulty = ch.Difficulty,
completedBy = ch.CompletedBy,
imgUrl = ch.ImgUrl,
subcategory = cc.Title,
category = cc.Title,
instructions = ch.Instructions
});
return challengesQ;
}
challengesQ
e q
sono alcune delle cose che ho provato fino ad ora, ma sto ricevendo questo o un errore simile:
CS0266 Impossibile convertire implicitamente il tipo 'System.Linq.IQueryable <>' in 'System.Collections.Generic.List'. Esiste una conversione esplicita (ti manca un cast?) Challenge.EF
Lo so perché ho bisogno dell'elenco IQueryable
o di un modo diverso di eseguire la query, ma non sono ancora riuscito a trovare la soluzione giusta. Qualcuno ha avuto un problema simile?
Sembra che il tuo problema principale sia che stai tentando di distribuire un IQueryable di tipo anonimo anziché un IQueryable of Challenges .
Idealmente, avresti fornito la definizione dei tuoi oggetti POCO (se code-first) o delle classi sotto .edmx se stai usando un'interfaccia di design per il tuo contesto EF. In questo modo possiamo verificare che stai effettivamente mappando le informazioni di join agli oggetti Sfida. Per quello che posso dire, lo stai facendo, e io risponderò a questa ipotesi; sarebbe bello essere sicuro però.
Sono stato in grado di definire il seguente esempio di EF semplice:
class Program
{
static ChallengeContext Context = null;
static void Main(string[] args)
{
try
{
var test = GetChallenges();
}
finally
{
Context.Dispose();
}
}
public static IQueryable<Challenge> GetChallenges()
{
Context = new ChallengeContext();
DbSet<Challenge> challenges = Context.Challenges;
DbSet<ChallengeCategory> categories = Context.Categories;
return challenges.Join(
categories,
ch => ch.Category,
cc => cc.Id,
(ch, cc) => new Challenge()
{
Id = ch.Id
});
}
}
internal class ChallengeContext:DbContext
{
public DbSet<Challenge> Challenges { get; set; }
public DbSet<ChallengeCategory> Categories { get; set; }
}
internal class ChallengeCategory
{
public int Id { get; set; }
}
public class Challenge
{
public int Id { get; set; }
public int Category { get; set; }
}
Il che mi suggerisce che il seguente dovrebbe funzionare:
public IQueryable<Challenges> GetChallenges()
{
DbSet<Challenges> challenges = challengeContext.Challenges;
DbSet<ChallengeCategories> categories = challengeContext.ChallengeCategories;
return challenges.Join(
categories,
ch => ch.Category,
cc => cc.Id,
(ch, cc) => new Challenges()
{
id = ch.Id,
title = ch.Title,
createdAt = ch.CreatedAt,
daysNeeded = ch.DaysNeeded,
reward = ch.Reward,
difficulty = ch.Difficulty,
completedBy = ch.CompletedBy,
imgUrl = ch.ImgUrl,
subcategory = cc.Title,
category = cc.Title,
instructions = ch.Instructions
});
}
Per favore fatemi sapere se questo non risolve il problema.
Stai restituendo il tipo anonimo. È necessario proiettare sul tipo effettivo. Sostituisci select new {
con select new Category {
var q = challenges.Join(
categories,
ch => ch.Category,
cc => cc.Id,
(ch, cc) => new {ch, cc}
).ToList().Select(x=> new Category
{
id = x.ch.Id,
title = x.ch.Title,
createdAt = x.ch.CreatedAt,
daysNeeded = x.ch.DaysNeeded,
reward = x.ch.Reward,
difficulty = x.ch.Difficulty,
completedBy = x.ch.CompletedBy,
imgUrl = x.ch.ImgUrl,
subcategory = x.cc.Title,
category = x.cc.Title,
instructions = x.ch.Instructions
});
O
var challengesQ = ( from ch in challengeContext.Challenges
join cc in challengeContext.ChallengeCategories
on ch.Category equals cc.Id
select new Category {
id = ch.Id,
title = ch.Title,
createdAt = ch.CreatedAt,
daysNeeded = ch.DaysNeeded,
reward = ch.Reward,
difficulty = ch.Difficulty,
completedBy = ch.CompletedBy,
imgUrl = ch.ImgUrl,
subcategory = cc.Title,
category = cc.Title,
instructions = ch.Instructions
}
);