LINQ根据来自不同列的查询选择一个字段

时间:2016-12-14 18:06:58

标签: c# linq asp.net-mvc-4

我正在尝试根据user.identity.name从我的数据库中检索字段。我尝试了以下但我似乎无法得到UserName或CustomerId的结果,但我得到User.Identity.Name的“jmcgee”。因此,当我希望它返回CustomerId(01d1)时,dealerId最终会变为空。

我做错了什么?看起来我在这里设置了与其他问题/示例相同的所有内容,msdn等。

UserProfile表:UserName = jmcgee; CustomerId = 01d1

var dealerId = db.UserProfiles
               .Where(d=>d.UserName.Equals(User.Identity.Name))
               .Select (d=>d.CustomerId);

var dealerId = from d in db.UserProfiles
               where d.UserName == User.Identity.Name
               select d.CustomerId;

var dealerId = (from d in db.UserProfiles
                where d.UserName == User.Identity.Name
                select d.CustomerId).SingleOrDefault();

我也试过FirstOrDefault无济于事,但这可能是我对如何设置它缺乏了解。我甚至尝试用“jmcgee”替换User.Identity.Name,看看是否可行。

以下是更多代码(如果缺少其他需要,请告诉我,我会添加它):

控制器

TintagliaContext db = new TintagliaContext();

        public ActionResult OpenQuotes(string searchString, string excludeString, string modelFilter, int? page)
        {   // var dealerId = "01D1628";

            var dealerId = (from d in db.UserProfiles
                         where d.UserName == User.Identity.Name
                         select d.CustomerId).SingleOrDefault();

            string context = _customerRepository.GetContext(dealerId);

           Tintaglia.Models.Filter filter;

            if (context == "coverpools")
            {
                filter = new Tintaglia.Models.Filter { IsSubmitted = false, Order = quote => quote.Date_Created, SearchString = searchString, ExcludeString = excludeString };
            }
            else
            { filter = new Tintaglia.Models.Filter { IsSubmitted = false, DealerId = dealerId, Order = quote => quote.Date_Created, SearchString = searchString, ExcludeString = excludeString };
            }
            var quotes = _configurationRepository.GetQuotes(filter);
            var model = new List<ConfigurationViewModel>();

            foreach (var quote in quotes)
            {
                var viewModel = new ConfigurationViewModel();
                viewModel = viewModel.MapModelToViewModel(quote);

            model.Add(viewModel);

            }
            ViewData.Model = model.ToPagedList(page ?? 1, 20); ;

            return View();
        }

ICustomerRepository

namespace Infotech.Coverpools.Portal.Tintaglia.Repositories.Interfaces
{
    public interface ICustomerRepository
    {
        string GetContext(string dealerId);
        //bool Login(string userName, string password);
    }
}

_customerRepository

namespace Infotech.Coverpools.Portal.Tintaglia.Repositories
{
    public class CustomerRepository : ICustomerRepository
    {
        //public string GetContext(string dealerId)
        public string GetContext(string dealerId)

        {
            using (var db = new TintagliaContext())
            {
                var customer = db.Customers.FirstOrDefault(x => x.No_ == dealerId);
                return customer.Internal_Login == 1 ? "coverpools" : "default";
            }
        }
}
}

TintagliaContext

namespace Infotech.Coverpools.Portal.Tintaglia.CodeFirst.Models
{
    public partial class TintagliaContext : DbContext
    {
        static TintagliaContext()
        {
            Database.SetInitializer<TintagliaContext>(null);
        }

        public TintagliaContext()
            : base("TintagliaContext")
        {
        }

        public DbSet<Configuration> Configurations { get; set; }
        public DbSet<Customer> Customers { get; set; }

        //New for Account Module
        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<webpages_UsersInRoles> webpages_UsersInRole { get; set; }
        // public DbSet<Customer> Customers { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new ConfigurationMap());
            modelBuilder.Configurations.Add(new CustomerMap());
        }
    }
}

0 个答案:

没有答案