-1

我正在尝试将下面提到的 SQL 查询转换为 LINQ 查询

Qry = "select Announcement, StartDate, Enddate,Annkey from
Announcements ";   Qry = Qry & " where Startdate <= #" & Today & "# " 
Qry = Qry & " and Enddate >= #" & Today & "# "  Qry = Qry + " order by
StartDate, Enddate";

到目前为止我所做的 -

public bool Selectdatafromannouncements()
        {


            List<Announcement> lstAnnounce = new List<Announcement>(
                from l in objVaccinationContext.Announcements
                where
                    l.StartDate.Value < DateTime.Today
                    && l.EndDate.Value < DateTime.Today
                orderby l.StartDate, l.EndDate
                select new (l.Announcement1, l.StartDate, l.EndDate, l.AnnouncementID)
                );


            return true;
        }

在 select new 附近出现错误(如预期的那样,请帮助我,我是 LINQ 的初学者

4

5 回答 5

2

您在这里不需要构造函数来创建列表。花括号用于创建匿名对象,因此不要声明 lstAnnounceList<Announcement>类型,最好使用 var 关键字。

所以你可以有这个:

var query = from l in objVaccinationContext.Announcements
                  where
                    l.StartDate.Value < DateTime.Today
                    && l.EndDate.Value < DateTime.Today
                orderby l.StartDate, l.EndDate
                select new {l.Announcement1, l.StartDate, l.EndDate, l.AnnouncementID };
var lstAnnounce = query.ToList();

如果您需要 Annoucement 对象列表:

var query = 
                from l in objVaccinationContext.Announcements
                where
                    l.StartDate.Value < DateTime.Today
                    && l.EndDate.Value < DateTime.Today
                orderby l.StartDate, l.EndDate
                select l;
List<Announcement> lstAnnounce = query.ToList();
于 2012-09-05T06:49:26.370 回答
1

如果objVaccinationContext.AnnouncementsAnnouncement对象的集合,则不需要匿名类型或使用 List 的构造,您可以这样做:

List<Announcement> lstAnnounce = (
    from l in objVaccinationContext.Announcements
    where
        l.StartDate.Value < DateTime.Today && 
        l.EndDate.Value < DateTime.Today
    orderby l.StartDate, l.EndDate
    select l)
    .ToList();
于 2012-09-05T06:44:29.677 回答
0

select new (l.Announcement1, l.StartDate, l.EndDate, l.AnnouncementID)

是错的

尝试匿名类型:

select new {var1 = l.Announcement1, var2 = l.StartDate, vr3 = l.EndDate, var4 = l.AnnouncementID}
于 2012-09-05T06:34:02.547 回答
0

问题是您将一个类型的项目插入到执行时anonymous的列表中。您需要做的是显式创建类型的实例:Announcementselect new (l.Announcement1, l.StartDate, l.EndDate, l.AnnouncementID)Announcement

List<Announcement> lstAnnounce = 
objVaccinationContext.Announcements
                     .OrderBy(l => l.StartDate)
                     .ThenBy(l => l.EndDate)
                     .Where(l => l.StartDate.Value < DateTime.Today &&
                                 l.EndDate.Value < DateTime.Today)
                     .Select(l => new Announcement
                     {
                         Announcement1 = l.Announcement1,
                         AnnouncementId = l.AnnouncementId,
                         EndDate = l.EndDate,
                         StartDate = l.StartDate
                     })
                     .ToList();
于 2012-09-05T06:44:08.810 回答
0

在您使用的原始查询中

Startdate <= Today  and Enddate >= Today 

而你的 Linq 有

l.StartDate.Value < DateTime.Today
&& l.EndDate.Value < DateTime.Today

假设您的首要查询是您所追求的,这对您有用吗:

 List<Announcement> lstAnnounce = 
            (from l in objVaccinationContext.Announcements
            where
                l.StartDate.Value <= DateTime.Today
                && l.EndDate.Value >= DateTime.Today
            orderby l.StartDate, l.EndDate
            select new Announcement
                 {
                     Announcement1 = l.Announcement1,
                     StartDate = l.StartDate,
                     EndDate = l.EndDate,
                     AnnouncementId = l.AnnouncementId
                 }).ToList();
于 2012-09-05T10:52:13.250 回答