我正在使用C#和.NET Framework 4.5.1使用Entity Framework 6.1.3从SQL Server数据库中检索数据.
我有这个:
codes = codesRepo.SearchFor(predicate)
.Select(c => new Tuple<string, byte>(c.Id, c.Flag))
.ToList();
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到此消息:
LINQ to Entities中仅支持无参数构造函数和初始值设定项.
我不知道我是如何创建元组的,因为我发现的所有例子大多都是这样的.
我试过这个:
codes = codesRepo.SearchFor(predicate)
.Select(c => Tuple.Create(c.Id, c.Flag))
.ToList();
Run Code Online (Sandbox Code Playgroud)
并得到此错误:
LINQ to Entities无法识别方法'System.Tuple`2 [System.String,System.Byte] Create [String,Byte](System.String,Byte)'方法,并且此方法无法转换为商店表达式.
问题出在哪儿?
也许一个简单的问题,但无法轻易找到它原谅我=)我尝试选择多个列.我使用的声明是:
var dataset2 = from recordset in entities.processlists
where recordset.ProcessName == processname
select recordset.ServerName, recordset.ProcessID, recordset.Username;
Run Code Online (Sandbox Code Playgroud)
显然,这甚至都没有编译.什么是正确的语法?我也试过基于方法,甚至强硬这个语法似乎是正确的,当访问它时抛出'无法强制转换类型'匿名类型'来键入'AIM.PInfo'.LINQ to Entities仅支持转换EDM原语或枚举类型.例外.
有任何想法吗?
var dataset = entities.processlists
.Where(x => x.environmentID == environmentid && x.ProcessName == processname && x.RemoteIP == remoteip && x.CommandLine == commandlinepart)
.Select(x => new { x.ServerName, x.ProcessID, x.Username })
.Cast<PInfo>().ToList();
Run Code Online (Sandbox Code Playgroud) var tasks = from i in data.Incidents
join a in data.Accounts on i.CustomerID equals a.Acct_CID
select new Tasks()
{
creator_id = a.ID,
start_date = i.DateOpened,
end_date = i.DateCLosed,
product_code = i.ProductCode,
install_type = i.InstallType,
os = i.OSType,
details = i.Description,
solution = i.Solution,
creator_name = i.TechID,
category = i.Title,
text = "Ticket for" + " " + i.Name,
status_id = 7
};
foreach (var task in tasks)
data.Tasks.Add(task);
data.SaveChanges();
public class Incidents
{
[Key]
public int …Run Code Online (Sandbox Code Playgroud) 在这里和这里阅读问题让我对这种情况有所了解,似乎使用AsEnumerable是内存消耗.有没有更好的方法来实现这个LINQ以及现在的方式,数据是否可靠?
删除AsEnumerable导致"除了Contains运算符之外,本地序列不能用于查询运算符的LINQ to SQL实现".
var results = from p in pollcards.AsEnumerable()
join s in spoils.AsEnumerable() on new { Ocr = p.OCR, fileName = p.PrintFilename } equals new { Ocr = s.seq, fileName = s.inputFileName }
where p.Version == null
orderby s.fileOrdering, s.seq
select new ReportSpoilsEntity
{
seq = s.seq,
fileOrdering = s.fileOrdering,
inputFileName = s.inputFileName,
Ocr = p.OCR,
ElectorName = p.ElectorName
};
Run Code Online (Sandbox Code Playgroud) 我有一个与MVC连接查询的问题,我不知道为什么.
无法在LINQ to Entities查询中构造实体或复杂类型"Tusofona_Website.Models.site_noticias".
我的控制器:
private TusofonaDBs db = new TusofonaDBs();
//
// GET: /DestaquesMain/
public ActionResult Index()
{
var query = (from sd in db.site_desquesnoticias
join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia
where sn.Destaque == 1
select new site_noticias {
CorpoNoticia = sn.CorpoNoticia,
TituloNoticia = sn.TituloNoticia
}).ToList();
//return View(db.site_desquesnoticias.ToList());
return View(query);
}
Run Code Online (Sandbox Code Playgroud)
我的型号:
public class site_destaquesnoticias
{
[Key]
public Int32 IDDestaque { get; set; }
public Int32 IDNoticia { get; set; }
public string Foto { get; set; …Run Code Online (Sandbox Code Playgroud) 我试图像在这个线程中做同样的事情,但我得到错误:
"System.Collections.Generic.IEnumerable"不包含"添加",并没有扩展方法"添加"接受型"System.Collections.Generic.IEnumerable"的第一个参数的定义可以找到(是否缺少using指令或汇编参考?)
这是我的代码:
[HttpPost]
public ActionResult Create(ANIME anime)
{
var db = new MainDatabaseEntities();
var newanime = new ANIME
{
ID_AN = anime.ID_AN,
TITLE_OR = anime.TITLE_OR,
TITLE_EN = anime.TITLE_EN,
GENRES = new List<GENRES>()
};
foreach (var selectedAnime in anime.GENRES.Where(c => c.isSelected))
{
var genre = new GENRES { ID_GE = selectedAnime.ID_GE };
db.GENRES.Attach(genre);
newanime.GENRES.Add(genre); <--- this is the error line
}
db.ANIME.Add(newanime);
db.SaveChanges();
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
ANIME:
public partial class ANIME
{
public int ID_AN { get; set; …Run Code Online (Sandbox Code Playgroud) 所以我在下面有一个LINQ查询:
var data= (from p in _db.P
join t in _db.TP on
p.Id equals t.PId
select
new TPE{
PId= t.PId,
TId= t.Id
}
).ToList();
return data;
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,它给了我一个错误:
The entity or complex type 'TPE' cannot be constructed in a LINQ to Entities query.
Run Code Online (Sandbox Code Playgroud)
我已经查看了其他正确执行此操作的LINQ查询,但我不知道为什么我的查询无效.任何见解?
以下代码显示错误的问题是什么
public IList<specialisation_sub> GetAllActiveSpecialisation_Sub(
int specialisationid)
{
var queryList = _db.specialisation_sub
.Where(obj => obj.isdeleted == false &&
obj.specialisationid == specialisationid)
.Select(obj => new specialisation_sub()
{
Id = obj.Id,
name = obj.name
}).ToList();
return queryList.ToList<specialisation_sub>();
}
Run Code Online (Sandbox Code Playgroud)
我需要如上所述仅选择两列
显示以下错误
无法在LINQ to Entities查询中构造实体或复杂类型"JobPortalModel.specialisation_sub".
我正在尝试运行这个简单的查询:
var appt = (from a in context.AppointmentSet
select new Appointment{ ModifiedOn = a.ModifiedOn}).First();
Run Code Online (Sandbox Code Playgroud)
但我收到编译器异常,因为 ModifiedOn 是只读的。
a,但随后Appointment将返回实体的所有属性,而不仅仅是ModifiedOn. new { a.ModifiedOn },但随后 appt 将是 anAnonymousType而不是Appointment.使这项工作的建议方法是什么?
请注意,这是一个示例,假设我从 Appointment 返回的不仅仅是一个属性,然后还有某种 where 条件