gpt4 book ai didi

c# - 在 EF 中使用 sql 查询

转载 作者:太空狗 更新时间:2023-10-29 19:40:26 25 4
gpt4 key购买 nike

我通过以下方式从数据库中获取数据:

 result = (from d in context.FTDocuments
join f in context.FTDocFlags on d.ID equals f.DocID into fgrp
from x in fgrp.DefaultIfEmpty()
where d.LevelID == levelID && x.UserID == userID && d.Status.Equals(DocumentStatus.NEW)
select new Entities.Document
{
ArrivalDate = d.ArrivalDate.Value,
BundleReference = d.BundleRef,
CreatedDate = d.CreatedDate,
CustomerID = d.CustomerID,
DocType = d.DocType.Value,
GuidID = d.DocGuid,
ID = d.ID,
LastExportID = d.LastExpID,
LevelID = d.LevelID,
ProfileID = d.ProfileID,
ScanDate = d.ScanDate.Value,
ScanOperator = d.ScanOperator,
SenderEmail = d.SenderEmail,
Status = d.Status,
VerifyOperator = d.VerOperator,
FlagNo = x == null ? 0 : x.FlagNo,
FlagUserID = x == null ? 0 : x.UserID
}).ToList();

现在,我尝试通过使用 sql 查询来实现这一点:

var test = context.Database.SqlQuery<string>("select *from FTDocument d left outer join FTDocFlag f on d.ID=f.DocID").ToList();

但是得到如下错误:

The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types

是否可以像上面那样使用复杂的查询?

我使用 EF 6.0。

最佳答案

您的查询没有返回单个字符串。像这样使用:

var test = context.Database.SqlQuery<Entities.Document>("...");

关于c# - 在 EF 中使用 sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50170950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com