Linq to sql Linq-2-Sql代码:这个可伸缩吗?

Linq to sql Linq-2-Sql代码:这个可伸缩吗?,linq-to-sql,Linq To Sql,我刚刚开始使用LINQtoSQL。我希望有人能验证linq-2-sql是否已将执行推迟到foreach循环执行。总而言之,有人能告诉我这个代码是否可以扩展吗。这是一个简单的get方法,只需几个搜索参数。谢谢 代码: public static IList<Content> GetContent(int contentTypeID, int feedID, DateTime? date, string text) { List<Conten

我刚刚开始使用LINQtoSQL。我希望有人能验证linq-2-sql是否已将执行推迟到foreach循环执行。总而言之,有人能告诉我这个代码是否可以扩展吗。这是一个简单的get方法,只需几个搜索参数。谢谢

代码:

public static IList<Content> GetContent(int contentTypeID, int feedID, DateTime? date, string text)
        {
            List<Content> contentList = new List<Content>();
            using (DataContext db = new DataContext())
            {
                var contentTypes = db.ytv_ContentTypes.Where(p => contentTypeID == -1 || p.ContentTypeID == contentTypeID);
                var feeds = db.ytv_Feeds.Where(p => p.FeedID == -1 || p.FeedID == feedID);

                var targetFeeds = from f in feeds
                                  join c in contentTypes on f.ContentTypeID equals c.ContentTypeID
                                  select new { FeedID = f.FeedID, ContentType = f.ContentTypeID };

                var content = from t in targetFeeds
                              join c in db.ytv_Contents on t.FeedID equals c.FeedID
                              select new { Content = c, ContentTypeID = t.ContentType };

                if (String.IsNullOrEmpty(text))
                {
                    content = content.Where(p => p.Content.Name.Contains(text) || p.Content.Description.Contains(text));
                }

                if (date != null)
                {
                    DateTime dateTemp = Convert.ToDateTime(date);
                    content = content.Where(p => p.Content.StartDate <= dateTemp && p.Content.EndDate >= dateTemp);
                }

                //Execution has been defered to this point, correct?
                foreach (var c in content)
                {
                    Content item = new Content()
                    {
                        ContentID = c.Content.ContentID,
                        Name = c.Content.Name,
                        Description = c.Content.Description,
                        StartDate = c.Content.StartDate,
                        EndDate = c.Content.EndDate,
                        ContentTypeID = c.ContentTypeID,
                        FeedID = c.Content.FeedID,
                        PreviewHtml = c.Content.PreviewHTML,
                        SerializedCustomXMLProperties = c.Content.CustomProperties
                    };
                    contentList.Add(item);
                }
            }
            //TODO
            return contentList;
        }
公共静态IList GetContent(int-contentTypeID、int-feedID、DateTime?日期、字符串文本)
{
列表内容列表=新列表();
使用(DataContext db=newdatacontext())
{
var contentTypes=db.ytv_contentTypes.Where(p=>contentTypeID==-1 | | p.contentTypeID==contentTypeID);
var feeds=db.ytv_feeds.Where(p=>p.FeedID==-1 | | p.FeedID==FeedID);
var targetFeeds=来自feeds中的f
在f.ContentTypeID等于c.ContentTypeID的contentTypes中加入c
选择新{FeedID=f.FeedID,ContentType=f.ContentTypeID};
var内容=来自targetFeeds中的t
在t.FeedID上的db.ytv_内容中加入c.FeedID等于c.FeedID
选择新{Content=c,ContentTypeID=t.ContentType};
if(String.IsNullOrEmpty(text))
{
content=content.Where(p=>p.content.Name.Contains(text)| | p.content.Description.Contains(text));
}
如果(日期!=null)
{
DateTime dateTemp=Convert.ToDateTime(日期);
content=content.Where(p=>p.content.StartDate=dateTemp);
}
//执行推迟到现在,对吗?
foreach(内容中的var c)
{
内容项=新内容()
{
ContentID=c.Content.ContentID,
Name=c.Content.Name,
Description=c.Content.Description,
StartDate=c.Content.StartDate,
EndDate=c.Content.EndDate,
ContentTypeID=c.ContentTypeID,
FeedID=c.Content.FeedID,
PreviewHtml=c.Content.PreviewHtml,
SerializedCustomXMLProperties=c.Content.CustomProperties
};
contentList.Add(项);
}
}
//待办事项
返回内容列表;
}

取决于您对“刻度”的理解。DB端如果处理大型表,此代码可能会导致问题;SQL Server的优化器在处理where子句谓词中的“or”运算符方面非常糟糕,如果存在多个谓词,则倾向于返回到表扫描。我会使用几个.Union调用来避免SQL仅仅因为| |而返回到表扫描的可能性

如果您可以共享有关基础表及其数据的更多详细信息,那么就更容易给出更详细的答案