如何加载另一个表并使用它 - 使用 linq to sql



我的数据库中有一个名为Profile的表,我有另一个名为ProfileConfirmed的表。此表的目的是确认此人确认了他的电子邮件地址。我希望当我遍历所有配置文件时,尚未确认的配置文件不会出现。

这是我的福尔奇

 @foreach (var c in Model.Profiles)
                            {
}

这是我在模型中获取配置文件的地方

 public List<Cleaner> GetProfiles()
        {
            using (var context = new CleanerDataContext(_connectionString))
            {
                var loadOptions = new DataLoadOptions();
                loadOptions.LoadWith<Cleaner>(c => c.ProfileConfirmed);
                context.LoadOptions = loadOptions;
                return context.Cleaners.ToList();
            }
        }

我建议您忽略第二个表,并在第一个表上添加一个带有位数据类型列的"IsConfirmed"。然后

var query = (from profiles in context.ProfileTable 
                where IsConfirmed == true select profiles).ToList();
return query;

更少的代码。更少的麻烦。

问题出在这一部分:

返回上下文。Cleaners.ToList((;

ToList(( 方法将始终获得数组的新副本,尽管其中的对象不是副本,但它们与原始数组中的引用相同。

public List<Cleaner> GetProfiles()
{
    using (var context = new CleanerDataContext(_connectionString))
        {
            var loadOptions = new DataLoadOptions();
            loadOptions.LoadWith<Cleaner>(c => c.ProfileConfirmed);
            context.LoadOptions = loadOptions;
            var confirmedProfilers =
                                  from cust in db.SomeTable
                                  where c.ProfileConfirmed == true
                                  select cust;
           return confirmedProfilers.ToList();
        }
    }

最新更新