首页 > 解决方案 > LINQ EF 和 VS2017

问题描述

我写了一个查询并在 LINQPAD 上工作

from x in FacilityData
from y in FavInformation
where y.UserID == 1 && x.ID == y.FacilityID 
select new
{
    xID = x.ID,
    xDistrictName = (from y in _Ilcelers
                     where y.ID == x.DistrictID
                     select y.IlceAd).FirstOrDefault(),
    xName = x.Name,
    Value = (from o in Tags
             from p in  Table_tags
             where o.Prefix != null && o.Prefix == p._NAME && o.Facility == y.FacilityID
             orderby p.İd descending
             select new
             {
                 FType = o.TagType,
                 Name = o.TagsName,
                 Value = p._VALUE,
                 Time = p._TIMESTAMP

             }).Take(Tags.Count(h => h.Facility == y.FacilityID))
}

结果

结果是完美的

但不适用于视觉工作室,

Value = (from o in DB.Tags
    from p in DB.table_tags
    where o.Prefix != null && o.Prefix == p.C_NAME && o.Facility == 11
    orderby p.id descending
    select new
    {
        FType=o.TagType,
        Name = o.TagsName,
        Value = p.C_VALUE,
        Time = p.C_TIMESTAMP
    }).Take(Tags.Count(h => h.Facility == y.FacilityID))

它给出了一个错误。我猜.Take()的部分不起作用,因为它是对 EF 的 linq。

错误:

Limit must be a DbConstantExpression or a Db Parameter Reference Expression. Parametre name: count]

错误图像

谢谢你有美好的一天

标签: linq

解决方案


不确定,但我会把它扔进去。如果你在谈论 linq to ef/sql,他们可能对 C# 一无所知。如果 take() 会成为问题,请尝试通过执行 .tolist() 来首先在本地获取选择结果。然后使用您的拍摄功能。

.ToList().Take(Tags.Count(h => h.Facility == y.FacilityID))

推荐阅读