.net 无法使用EF从sql中检索具体数据

pgx2nnw8  于 2023-03-09  发布在  .NET
关注(0)|答案(2)|浏览(134)

我正在编码一些数据过滤器,但我有一个问题。不能得到数据,不知道什么是错的。我已经尝试了5个小时了。
这是我的代码:

var context = _contextFactory.CreateDbContext();

            //select records
            var dok = await context.Dok
            .Where(x => x.Data == DateTime.Today.AddDays(-2) && x.TypDok == 21)
            .ToListAsync();

            // Define the DokId (column name) range from dok
            var dokIdRange = dok.Select(x => x.DokId);

            //select records containing dokId
            var pozDok = await context.PozDok
                          .Where(x => dokIdRange.Contains(x.DokId))
                         .ToListAsync();

            //group them and count
            var result = (
                    from pd in pozDok
                    group pd by pd.TowId into g
                    select new
                    {
                        TowId = g.Key,
                        IloscPlusSum = g.Sum(x => x.IloscPlus)
                    }
                ).ToList();

你知道出什么事了吗?
这种情况是:我从名为dok的表中获取特定日期的记录,然后检查dokid范围。
然后我调用pozdok表并从dokidrange中获取所有包含dokid的记录。然后我需要将towid变成1个对象并添加所有的iloscplus计数。(可以有3x个towid对象,我需要将其中3个对象的iloscplus = count作为一个对象。)

更新

这里是Dok和PozDok类:

public class Dok
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    public decimal DokId { get; set; }
    public decimal UzId { get; set; }
    public decimal MagId { get; set; }
    public DateTime Data { get; set; }
    public int KolejnyWDniu { get; set; }
    public DateTime DataDod { get; set; }
    public DateTime DataPom { get; set; }
    public string NrDok { get; set; }
    public short TypDok { get; set; }
    public short Aktywny { get; set; }
    public short Opcja1 { get; set; }
    public short Opcja2 { get; set; }
    public short Opcja3 { get; set; }
    public short Opcja4 { get; set; }
    public short CenyZakBrutto { get; set; }
    public short CenySpBrutto { get; set; }
    public short FormaPlat { get; set; }
    public short TerminPlat { get; set; }
    public short PoziomCen { get; set; }
    public decimal RabatProc { get; set; }
    public decimal Netto { get; set; }
    public decimal Podatek { get; set; }
    public decimal NettoUslugi { get; set; }
    public decimal PodatekUslugi { get; set; }
    public decimal NettoDet { get; set; }
    public decimal PodatekDet { get; set; }
    public decimal NettoDetUslugi { get; set; }
    public decimal PodatekDetUslugi { get; set; }
    public decimal NettoMag { get; set; }
    public decimal PodatekMag { get; set; }
    public decimal NettoMagUslugi { get; set; }
    public decimal PodatekMagUslugi { get; set; }
    public decimal Razem { get; set; }
    public decimal DoZaplaty { get; set; }
    public decimal Zaplacono { get; set; }
    public decimal Kwota1 { get; set; }
    public decimal Kwota2 { get; set; }
    public decimal Kwota3 { get; set; }
    public decimal Kwota4 { get; set; }
    public decimal Kwota5 { get; set; }
    public decimal Kwota6 { get; set; }
    public decimal Kwota7 { get; set; }
    public decimal Kwota8 { get; set; }
    public decimal Kwota9 { get; set; }
    public decimal Kwota10 { get; set; }
    public int Param1 { get; set; }
    public int Param2 { get; set; }
    public int Param3 { get; set; }
    public int Param4 { get; set; }
    public short EksportFK { get; set; }
    public DateTime Zmiana { get; set; }
    public int? NrKolejny { get; set; }
    public int? NrKolejnyMag { get; set; }
    public int? Param5 { get; set; }
    public int? Param6 { get; set; }
    public decimal? Kwota11 { get; set; }
    public decimal? Kwota12 { get; set; }
    public int? WalId { get; set; }
    public decimal? Kurs { get; set; }
    public decimal? CentrDokId { get; set; }
    public short? Opcja5 { get; set; }
    public short? Opcja6 { get; set; }
    public short? Opcja7 { get; set; }
    public short? Opcja8 { get; set; }
    public DateTime? ZmianaPkt { get; set; }
    public decimal? ZaplaconoPodatek { get; set; }
    public decimal? ZaplaconoWKasie { get; set; }
}



public class PozDok
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    public decimal DokId { get; set; }
    public int Kolejnosc { get; set; }
    public int NrPozycji { get; set; }
    public decimal TowId { get; set; }
    public short TypPoz { get; set; }
    public decimal IloscPlus { get; set; }
    public decimal IloscMinus { get; set; }
    public short PoziomCen { get; set; }
    public short Metoda { get; set; }
    public decimal CenaDomyslna { get; set; }
    public decimal CenaPrzedRab { get; set; }
    public decimal RabatProc { get; set; }
    public decimal CenaPoRab { get; set; }
    public decimal Wartosc { get; set; }
    public decimal CenaDet { get; set; }
    public decimal CenaMag { get; set; }
    public short Stawka { get; set; }
    public short TypTowaru { get; set; }
    public decimal IleWZgrzewce { get; set; }
    public short? StawkaDod { get; set; }
    public decimal? Netto { get; set; }
    public decimal? Podatek { get; set; }
}

我需要按数据过滤Dok,我得到的DokId列表。
现在,我需要过滤PozDok的DokId列表,我得到的结果列表。
现在我需要加入TowId(许多TowId)并计算它们的IloscPlus

f3temu5u

f3temu5u1#

这是正确答案!:)我自己解决了这个问题:)

var q = (from dok1 in context.Dok.Where(a => a.Data >= DateTime.Today.AddDays(-7))   
         from pozDok2 in context.PozDok.Where(a => a.DokId == dok1.DokId )
                group pozDok2 by pozDok2.TowId into g
                select new
                {
                    TowId = g.Key,
                    IloscPlusSum = g.Sum(x => x.IloscPlus)
                }
            ).ToList();
kpbwa7wx

kpbwa7wx2#

根据您提供的代码,您的操作似乎是正确的。但是,有几个因素可能会导致此问题:
验证Dok表和PozDok表是否具有在实体框架模型中定义的关系。这对于Contains方法在基于从Dok表获取的DokId值筛选PozDok表时正常工作是必需的。

相关问题