EntityFramework多个问题所在

时间:2014-02-13 14:00:59

标签: c# entity-framework jqgrid

我在我的后端使用Entity Framework并在我的前端使用jqGrid。我的网格看起来像这样:

Filterrow

如果我使用其中一个过滤器,我的网格工作正常。 例如:

使用过滤器:“Erfasser” Using one Filter ("erfasser" 使用过滤器:“Land” Using one other Filter ("Land")

两个过滤器都工作得很好,但共同点我有以下结果:

Both Filters, but no Results

我对这个问题一无所知。这是我的代码:

    using (var db = new ProconContext())
    {
        // Paging für die Liste erstellen, da zuviele Datensätze, user sollte selber über die 
        // größe der Seite bestimmen können.
        var query = db.Belege.Join(db.Kunden, x => x.KundenNr, y => y.KundeId, (x, y) => new { Beleg = x, Kunde = y })
            .Select(x => new BelegKunden { Beleg = x.Beleg, Kunde = x.Kunde });

        db.Configuration.AutoDetectChangesEnabled = false;

        query = query.Where(x => x.Beleg.BelegTypId == belegArtNr);

        foreach (var param in parameters)
        {
            if (param.Key.Equals("BelegId"))
            {
                var belegId = Convert.ToInt32(param.Value);
                query = query.Where(x => x.Beleg.BelegId == belegId);
            }

            if (param.Key == "Name")
            {
                query = query.Where(x => x.Kunde.Name.Contains(param.Value));
            }

            if (param.Key.Equals("Datum"))
            {
                query = query.Where(x => x.Datum.Equals(param.Value));
            }

            if (param.Key.Equals("KundenNr"))
            {
                var kundenNr = Convert.ToInt32(param.Value);
                query = query.Where(x => x.Beleg.KundenNr == kundenNr);
            }

            if (param.Key.Equals("Land"))
            {
                query = query.Where(x => x.Kunde.Land.ZweiBuchstabenIsoCode.Contains(param.Value));
            }

            if (param.Key.Equals("PLZ"))
            {
                query = query.Where(x => x.Kunde.Plz.Contains(param.Value));
            }

            if (param.Key.Equals("Ort"))
            {
                query = query.Where(x => x.Kunde.Ort.Contains(param.Value));
            }

            if (param.Key.Equals("Erfasser"))
            {
                query = query.Where(x => x.Beleg.ErfasstVonUsername.Contains(param.Value));
            }

            if (param.Key.Equals("Kommission"))
            {
                query = query.Where(x => x.Beleg.KopfKommission.Contains(param.Value));
            }

            if (param.Key.Equals("ErstePosition"))
            {
                query = query.Where(x => x.Beleg.ErsteRechnungsPosition.Contains(param.Value));
            }
        }

        var count = query.Count();

        var list = query.OrderByDescending(x => x.Beleg.BelegDatum)
            .Skip(skip) // Wie viele soll er überspringen (die ersten x)
            .Take(take) // Wie viele soll er von da aus nehmen (nimm x)
            .ToList();

        list[0].AllRowsCount = count;
        return AppServerHelper.AppResponse(list);
    }
}
catch (Exception e)
{
    return AppServerHelper.AppResponse(new List<BelegKunden>());
}

On Runtime:计数结果适用于一个过滤器,但它不适用于2.

我正在使用: jqGrid,EntityFramework

编辑:

Entity Framwework的自动生成代码在服务器上正常工作为SQL语句。如果我复制自动生成的代码并在服务器上执行它我得到了正确的结果,对于两个过滤器。

编辑:

我的模特:

[Serializable]
public partial class Beleg
{
    [LocalizedDisplayName("BelegNr", ModelName = "tbWBI_Reklamationen")]
    public Int32 BelegId { get; set; }

    /// <summary>
    /// Veraltet, BelegId ist nun der Primary Key
    /// </summary>                
    public Int32 AlteBelegId { get; set; }

    public Int16 BelegTypId { get; set; }
    public virtual BelegTyp BelegTyp { get; set; }

    public Int16? LetzterGedruckterBelegTypId { get; set; }
    public virtual BelegTyp LetzterGedruckterBelegTyp { get; set; }

    public Int32 KundenNr { get; set; }

    [LocalizedDisplayName("BelegDatum", ModelName = "tbVKBelege")]
    public DateTime BelegDatum { get; set; }


    public String KopfKommission { get; set; }
    public String ErsteRechnungsPosition { get; set; }
    public String ErfasstVonUsername { get; set; }
    public Double WarenwertBrutto { get; set; }
}

配置:

public class Beleg_Config : EntityTypeConfiguration<Beleg>
{
    public Beleg_Config()
    {
        ToTable("tbVKBelege");

        HasKey(a => a.BelegId);
        Property(a => a.BelegId)
            .HasColumnName("BelegNr")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .IsRequired();

        Property(a => a.AlteBelegId).HasColumnName("");

        Property(a => a.BelegTypId).HasColumnName("");

        Property(a => a.LetzterGedruckterBelegTypId).HasColumnName("");

        Property(a => a.ErsteRechnungsPosition).HasColumnName("");

        Property(a => a.ErfasstVonUsername).HasColumnName("");

        Property(a => a.WarenwertBrutto).HasColumnName("");


        HasRequired(b => b.BelegTyp).WithMany().HasForeignKey(b => b.BelegTypId);

        HasOptional(b => b.LetzterGedruckterBelegTyp).WithMany().HasForeignKey(b => b.LetzterGedruckterBelegTypId);            
    }
}

昆德的配置:

public Kunde_Config()
{
    ToTable("tbKunden");

    HasKey(a => a.KundeId);
    Property(a => a.KundeId).HasColumnName("KundenNr").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

    /*Property(a => a.KundeId)
        .HasColumnName("KundenNr")
        .
        .IsRequired();*/

    Property(a => a.AussendienstlerId).HasColumnName("");

    Property(x => x.SpezialSbUserId).HasColumnName("");

    Property(a => a.Name).HasColumnName("");

    Property(a => a.Name2).HasColumnName("");

    Property(a => a.Straße).HasColumnName("");

    Property(a => a.TelefonNr).HasColumnName("");

    Property(a => a.FaxNr).HasColumnName("");

    Property(a => a.Website).HasColumnName("");

    Property(a => a.KeyAccountManagerId).HasColumnName("");

    Property(a => a.KWFP).HasColumnName("KWFP");

    Property(a => a.IOHomeControl).HasColumnName("IOHomeControl");

    Property(a => a.TopPartner).HasColumnName("");

    Property(x => x.ZahlungsbedingungId).HasColumnName("");

    Property(x => x.EShop).HasColumnName("");

    Property(x => x.EShopUsername).HasColumnName("");

    Property(x => x.EShopPasswort).HasColumnName("");

    Property(x => x.Quelle).HasColumnName("");

    Property(x => x.Kommentar).HasColumnName("");

    Property(x => x.Rabatt).HasColumnName("");

    Property(x => x.IstBaanKunde).HasColumnName("");

    Property(x => x.HatAltschulden).HasColumnName("");

    Property(x => x.StandardLieferadressenNr).HasColumnName("");

    Property(x => x.IstWettbewerber).HasColumnName("");

    HasRequired(b => b.AußendienstMitarbeiter).WithMany().HasForeignKey(b => b.k);
    HasRequired(b => b.Land).WithMany().HasForeignKey(b => b.LandId);
    HasOptional(b => b.KeyAccountManager).WithMany().HasForeignKey(b => b.k);
    // HasOptional(x => x.Messedaten);
}

我删除了配置数据中的“columnnames”作为安全,对不起。

2 个答案:

答案 0 :(得分:1)

我相信我遇到了你的问题,当你进行连接时,你会以错误的方式创建一个匿名类型,这样做:

var query = db.Beleg.Join(
                db.Kunden,
                x => new { x.KundenNr }, //the outer selector, the foreing key to Kunden
                y => new { y.KundeId }, //the inner selector, the primary key in Kunde, this will match x.KundenNr == y.KundenId
                (x,y) => new { BelegNr = x.BelegId , BelegDatum = x.BelegDatum, Name = y.Name .....}); //here you select the columns(fields) in each table, and construct the new anonymous object with the fields that you need in each table

以下是一些可以帮助您更深入理解的链接

http://msdn.microsoft.com/en-us/library/bb534675(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/bb534675%28v=vs.110%29.aspx http://www.w3schools.com/sql/sql_join.asp

LINQ: How to do JOIN using the linq extension method style on multiple fields?

我是analazing,在你的情况下,你不需要一个连接,一个连接用于选择你要加入的每个表的几个字段,但在这里你保留两个entires表。我的建议是使用简单的where子句。

希望它有所帮助。

答案 1 :(得分:1)

首先:感谢emcas88给你时间和帮助!

我用你的想法解决了问题: 仅选择字段AND:

var query = db.Beleg.Join(
                db.Kunden,
                x => new { x.KundenNr }, //the outer selector, the foreing key to Kunden
                y => new { y.KundeId }, //the inner selector, the primary key in Kunde, this will match x.KundenNr == y.KundenId
                (x,y) => new { BelegNr = x.BelegId , BelegDatum = x.BelegDatum, Name = y.Name .....}); //here you select the columns(fields) in each table, and construct the new anonymous object with the fields that you need in each table

另一个问题是: 我需要改变这个:

foreach (var param in parameters)
    {
        if (param.Key.Equals("BelegId"))
        {
            var belegId = Convert.ToInt32(param.Value);
            query = query.Where(x => x.Beleg.BelegId == belegId);
        }

        if (param.Key == "Name")
        {
            query = query.Where(x => x.Kunde.Name.Contains(param.Value));
        }

        if (param.Key.Equals("Datum"))
        {
            query = query.Where(x => x.Datum.Equals(param.Value));
        }

        if (param.Key.Equals("KundenNr"))
        {
            var kundenNr = Convert.ToInt32(param.Value);
            query = query.Where(x => x.Beleg.KundenNr == kundenNr);
        }

        if (param.Key.Equals("Land"))
        {
            query = query.Where(x => x.Kunde.Land.ZweiBuchstabenIsoCode.Contains(param.Value));
        }

        if (param.Key.Equals("PLZ"))
        {
            query = query.Where(x => x.Kunde.Plz.Contains(param.Value));
        }

        if (param.Key.Equals("Ort"))
        {
            query = query.Where(x => x.Kunde.Ort.Contains(param.Value));
        }

        if (param.Key.Equals("Erfasser"))
        {
            query = query.Where(x => x.Beleg.ErfasstVonUsername.Contains(param.Value));
        }

        if (param.Key.Equals("Kommission"))
        {
            query = query.Where(x => x.Beleg.KopfKommission.Contains(param.Value));
        }

        if (param.Key.Equals("ErstePosition"))
        {
            query = query.Where(x => x.Beleg.ErsteRechnungsPosition.Contains(param.Value));
        }
    }

进入这个:

if (parameters.ContainsKey("BelegId"))
                {
                    var belegId = Convert.ToInt32(parameters["BelegId"]);
                    query = query.Where(x => x.BelegId == belegId);
                }

                if (parameters.ContainsKey("Name"))
                {
                    var tmp = parameters["Name"];
                    query = query.Where(x => x.Name.Contains(tmp));
                }

                if (parameters.ContainsKey("Datum"))
                {
                    var tmp = parameters["Name"];
                    query = query.Where(x => x.BelegDatum.Equals(tmp));
                }

                if (parameters.ContainsKey("KundenNr"))
                {
                    var kundenNr = Convert.ToInt32(parameters["KundenNr"]);
                    query = query.Where(x => x.KundenNr == kundenNr);
                }

                if (parameters.ContainsKey("Land"))
                {
                    var tmp = parameters["Land"];
                    query = query.Where(x => x.Land.ZweiBuchstabenIsoCode.Contains(tmp));
                }

                if (parameters.ContainsKey("PLZ"))
                {
                    var tmp = parameters["PLZ"];
                    query = query.Where(x => x.Plz.Contains(tmp));
                }

                if (parameters.ContainsKey("Ort"))
                {
                    var tmp = parameters["Ort"];
                    query = query.Where(x => x.Ort.Contains(tmp));
                }

                if (parameters.ContainsKey("Erfasser"))
                {
                    var tmp = parameters["Erfasser"];
                    query = query.Where(x => x.ErfasstVonUsername.Contains(tmp));
                }

                if (parameters.ContainsKey("Kommission"))
                {
                    var tmp = parameters["Kommission"];
                    query = query.Where(x => x.KopfKommission.Contains(tmp));
                }

                if (parameters.ContainsKey("ErstePosition"))
                {
                    var tmp = parameters["ErstePosition"];
                    query = query.Where(x => x.ErsteRechnungsPosition.Contains(tmp));
                }

我猜问题是foreach循环。 循环执行查询两次,在第二个代码块中,查询在函数结束时执行。