c#Linq和SQL

时间:2015-12-17 14:40:01

标签: c# sql entity-framework linq

我尝试从MSSQL-Database中使用EF6获取一些数据:

IQueryable<Kundentree1> tmp = from f in db.Kundentree1 select f ;

Kundentree1是数据库中的SQL-View

因为我没有完成所有结果,所以我会过滤结果:

tmp = tmp.Where(k => k.Strasse
                    .ToLower()
                    .Trim().Contains(search.Strasse
                                           .ToLower()
                                           .Trim()))
         .Distinct();

(这不是唯一的,还有一些......)

在我的程序结束时:

var sql = tmp.ToString();
return View(tmp);

在我的测试例程中,tmp返回7项作为结果,但项目0 - 6是相同的。

如果我使用SqlStatement(SQL),我也会获得7个项目,但在这种情况下,正确的结果是7个不同的项目。

任何可能出现问题的提示?

更新

完整生成的SQL-CODE(并正确工作)

SELECT 
    [Extent1].[Firmenname] AS [Firmenname], 
    [Extent1].[rechtsform] AS [rechtsform], 
    [Extent1].[Strasse] AS [Strasse], 
    [Extent1].[Land] AS [Land], 
    [Extent1].[Postleitzahl] AS [Postleitzahl], 
    [Extent1].[Ort] AS [Ort], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[Personen_ID] AS [Personen_ID], 
    [Extent1].[Firmen_ID] AS [Firmen_ID], 
    [Extent1].[Adressen_ID] AS [Adressen_ID], 
    [Extent1].[sex] AS [sex], 
    [Extent1].[Vorwahl] AS [Vorwahl], 
    [Extent1].[Tel1] AS [Tel1], 
    [Extent1].[zahlungskondition] AS [zahlungskondition], 
    [Extent1].[sperrkennzeichen] AS [sperrkennzeichen], 
    [Extent1].[sendkatalag] AS [sendkatalag], 
    [Extent1].[lastkatalog] AS [lastkatalog], 
    [Extent1].[ID] AS [ID], 
    [Extent1].[NACHNAME] AS [NACHNAME], 
    [Extent1].[VORNAME] AS [VORNAME], 
    [Extent1].[ZKTEXT] AS [ZKTEXT], 
    [Extent1].[FAX] AS [FAX], 
    [Extent1].[KNR] AS [KNR], 
    [Extent1].[UID] AS [UID], 
    [Extent1].[firmenbuch] AS [firmenbuch], 
    [Extent1].[email] AS [email], 
    [Extent1].[Lieferant] AS [Lieferant], 
    [Extent1].[PERS_VORNAME] AS [PERS_VORNAME], 
    [Extent1].[PERS_NACHNAME] AS [PERS_NACHNAME], 
    [Extent1].[PERS_sex] AS [PERS_sex], 
    [Extent1].[Titel] AS [Titel], 
    [Extent1].[Zusatz] AS [Zusatz], 
    [Extent1].[Position] AS [Position], 
    [Extent1].[PERS_EMAIL] AS [PERS_EMAIL], 
    [Extent1].[PERSONEN_GRUPPE_ID] AS [PERSONEN_GRUPPE_ID], 
    [Extent1].[NEWSLETTER] AS [NEWSLETTER], 
    [Extent1].[HEROLD_ID] AS [HEROLD_ID], 
    [Extent1].[INFOTEXT] AS [INFOTEXT], 
    [Extent1].[DISG] AS [DISG], 
    [Extent1].[HANDEL] AS [HANDEL], 
    [Extent1].[AdressenTYP] AS [AdressenTYP], 
    [Extent1].[BUNDESLAND] AS [BUNDESLAND], 
    [Extent1].[CANBOX] AS [CANBOX], 
    [Extent1].[KatalogTyp] AS [KatalogTyp], 
    [Extent1].[KatalogBezeichnung] AS [KatalogBezeichnung]
    FROM (SELECT 
    [Kundentree1].[Firmenname] AS [Firmenname], 
    [Kundentree1].[rechtsform] AS [rechtsform], 
    [Kundentree1].[Strasse] AS [Strasse], 
    [Kundentree1].[Land] AS [Land], 
    [Kundentree1].[Postleitzahl] AS [Postleitzahl], 
    [Kundentree1].[Ort] AS [Ort], 
    [Kundentree1].[Name] AS [Name], 
    [Kundentree1].[Personen_ID] AS [Personen_ID], 
    [Kundentree1].[Firmen_ID] AS [Firmen_ID], 
    [Kundentree1].[Adressen_ID] AS [Adressen_ID], 
    [Kundentree1].[sex] AS [sex], 
    [Kundentree1].[Vorwahl] AS [Vorwahl], 
    [Kundentree1].[Tel1] AS [Tel1], 
    [Kundentree1].[zahlungskondition] AS [zahlungskondition], 
    [Kundentree1].[sperrkennzeichen] AS [sperrkennzeichen], 
    [Kundentree1].[sendkatalag] AS [sendkatalag], 
    [Kundentree1].[lastkatalog] AS [lastkatalog], 
    [Kundentree1].[ID] AS [ID], 
    [Kundentree1].[NACHNAME] AS [NACHNAME], 
    [Kundentree1].[VORNAME] AS [VORNAME], 
    [Kundentree1].[ZKTEXT] AS [ZKTEXT], 
    [Kundentree1].[FAX] AS [FAX], 
    [Kundentree1].[KNR] AS [KNR], 
    [Kundentree1].[UID] AS [UID], 
    [Kundentree1].[firmenbuch] AS [firmenbuch], 
    [Kundentree1].[email] AS [email], 
    [Kundentree1].[Lieferant] AS [Lieferant], 
    [Kundentree1].[PERS_VORNAME] AS [PERS_VORNAME], 
    [Kundentree1].[PERS_NACHNAME] AS [PERS_NACHNAME], 
    [Kundentree1].[PERS_sex] AS [PERS_sex], 
    [Kundentree1].[Titel] AS [Titel], 
    [Kundentree1].[Zusatz] AS [Zusatz], 
    [Kundentree1].[Position] AS [Position], 
    [Kundentree1].[PERS_EMAIL] AS [PERS_EMAIL], 
    [Kundentree1].[PERSONEN_GRUPPE_ID] AS [PERSONEN_GRUPPE_ID], 
    [Kundentree1].[NEWSLETTER] AS [NEWSLETTER], 
    [Kundentree1].[HEROLD_ID] AS [HEROLD_ID], 
    [Kundentree1].[INFOTEXT] AS [INFOTEXT], 
    [Kundentree1].[DISG] AS [DISG], 
    [Kundentree1].[HANDEL] AS [HANDEL], 
    [Kundentree1].[AdressenTYP] AS [AdressenTYP], 
    [Kundentree1].[BUNDESLAND] AS [BUNDESLAND], 
    [Kundentree1].[CANBOX] AS [CANBOX], 
    [Kundentree1].[KatalogTyp] AS [KatalogTyp], 
    [Kundentree1].[KatalogBezeichnung] AS [KatalogBezeichnung]
    FROM [dbo].[Kundentree1] AS [Kundentree1]) AS [Extent1]
    WHERE (( CAST(CHARINDEX(LTRIM(RTRIM(LOWER('Salv'))), LTRIM(RTRIM(LOWER([Extent1].[Firmenname])))) AS int)) > 0) AND ( NOT (([Extent1].[Postleitzahl] IS NULL) OR ((LEN([Extent1].[Postleitzahl])) = 0))) AND (( CAST(CHARINDEX(LTRIM(RTRIM('4482')), LTRIM(RTRIM([Extent1].[Postleitzahl]))) AS int)) > 0)

1 个答案:

答案 0 :(得分:2)

感谢Ivan Stoev:

在数据库工作视图上创建密钥

在我的情况下,它是我需要工作的假密钥(uniqueidentifier newID())