Ok, last edit, promise:
With the following class:
public partial class MembershipModule : BaseConnection<MembershipModule>
{
private const string AccessPrivilege = "Access";
public bool Accessible()
{
return this.Privileges().Any(p => p.Name.Equals(AccessPrivilege));
}
public IQueryable<MembershipAction> Privileges()
{
var privileges = from p in LinqUtil.Context.MembershipModuleActions
where
p.MembershipModule.Id.Equals(this.Id) &&
p.MembershipRolePrivileges.Any(rp => rp.ModuleActionId.Equals(p.Id))
select p.MembershipAction;
return privileges;
}
}
Why does this work
public static List<MembershipModule> Collection()
{
List<MembershipModule> collection = new List<MembershipModule>();
if(!MembershipUser.Connected)
return collection;
foreach(MembershipModule m in LinqUtil.Context.MembershipModules)
{
if(m.Accessible())
collection.Add(m);
}
return collection;
}
While this doesn't?
public static List<MembershipModule> Collection()
{
if(!MembershipUser.Connected)
return new List<MembershipModule>();
return LinqUtil.Context.MembershipModules.Where(m => m.Accessible()).ToList();
}
looks to me like you are trying to execute a linq to object operation and it is trying to convert it to a sql statement.
Keep in mind: If this were to succede, I expect that the sql related to the folowing call would happen once per MembershipModule:
return this.Privileges().Any(p => p.Name.Equals(AccessPrivilege));
Have you tried skipping using the Accesible method, and hitting the Privileges collection directly? Something like this the code below. I have changed the return statement. This is the gist, may not be 100% correct:
public static List<MembershipModule> Collection()
{
if(!MembershipUser.Connected)
return new List<MembershipModule>();
var mod = MembershipModule.SearchById(new Guid("b012d35f-6af1-47de-9e54-e5df957c07e1"));
var y = from p in LinqUtil.Context.MembershipModuleActions
where
p.MembershipModule.Id.Equals(mod.Id) &&
p.MembershipRolePrivileges.Any(rp => rp.ModuleActionId.Equals(p.Id))
select p.MembershipAction;
var u = y.Any(p => p.Name.Equals(AccessPrivilege));
return LinqUtil.Context.MembershipModules.Where(m => m.Privileges().Any(p => p.Name.Equals(AccessPrivilege)).ToList();
}
Could it have to do with a static method (public static List<_membershipModule> Collection()) of a class trying access instance members (AccessPrivilege)?
Related
We have some repo methods, and I see a lot of our team using our GetAll() method over another (Get()):
Example use case:
var user = _uow.UserRepository.GetAll().Include(x => x.UserMap).Include(x=>x.UserFile).Include(x=>x.UserDomains).FirstOrDefault(x => emails.Contains(x.Email));
I'm wondering if instead they be using this one?
var user = _uow.UserRepository.Get(x => emails.Contains(x.Email), "UserMap,UserFile,UserDomains");
Here are those two methods:
public virtual TEntity Get(Expression<Func<TEntity, bool>> filter = null, string includeTables = "")
{
IQueryable<TEntity> query = _entities;
if (filter != null)
query = query.Where(filter);
foreach (var includeTable in includeTables.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeTable);
}
return query.FirstOrDefault();
}
public IQueryable<TEntity> GetAll()
{
return _entities.AsQueryable();
}
Is the Get() method more efficient than the GetAll() method? In my mind, the GetAll() method is calling everything in the table (we're using a Repository pattern with one repository per table), then narrowing down afterward, whereas the Get() is being more specific.
Can anyone tell me if my intuition is right or wrong about this? I'm wondering how efficient that GetAll() is given the use-case.
I am trying to create a linq statement to filter otu results using an any clause. My issue is that I dont have a single value to compare against.
In the example below I have a PropertyTaxBill entity that is the parent. Each one has a collection of TaxPropertyAssessmentDetails attached to it.
In this query people can specify that they only want to deal with bills pertaining to a specific class strata so I check to see if any values exist in the classStrata variable. If so then the user selected specific ones. I was trying to do an any clause on the classStrata but instead of giving it a single value to match on I was trying to select all the values in the TaxPropertyAssessmentDetails collection attached to the PropertyTaxBill. Is this possible?
using (var dataContext = contextProvider.GetContext())
{
var query = dataContext.PropertyTaxBills.Where(x => x.Id > 1);
var classStrata = new int[0];
if (classStrata != null && classStrata.Any())
{
query = query.Where(x => classStrata.Any(y => y == x.TaxPropertyAssessmentDetails.SelectMany(z => z.PropertyTaxClassStrataId)));
}
}
You need to use contains so that ef is able to translate to a ef query.
Not sure that i understood your model correctly. Here's a example with a model. Please tell me if i understood incorrect so i can fix it.
public void TestMethod1()
{
IEnumerable<TaxBill> TaxBills = new List<TaxBill>();
var query = TaxBills.Where(x => x.Id > 1);
var classStrata = new int[0];
if (classStrata != null && classStrata.Any())
{
query = query.Where(x => x.AssessmentDetails.Any(ad => ad.TaxClassStrataId.Any(cs => classStrata.Contains(cs))));
}
}
And the entities
public class TaxBill
{
public int Id { get; set; }
public ICollection<AsessmentDetails> AssessmentDetails { get; set; }
}
public class AsessmentDetails
{
public ICollection<int> TaxClassStrataId { get; set; }
}
I have an ObservableCollection like the following-
private ObservableCollection<KeyedList<int, Anime>> _grp;
public ObservableCollection<KeyedList<int, Anime>> GroupedAnimeByGenre
{
get
{
return _grp;
}
set
{
_grp = value;
RaisePropertyChanged("GroupedAnimeByGenre");
}
}
I am using this to populate a LongListSelector with grouping. The KeyedList is implemented like this-
public class KeyedList<TKey, TItem> : List<TItem>
{
public TKey Key { protected set; get; }
public KeyedList(TKey key, IEnumerable<TItem> items)
: base(items)
{
Key = key;
}
public KeyedList(IGrouping<TKey, TItem> grouping)
: base(grouping)
{
Key = grouping.Key;
}
}
I have the following code to feed the ObservableCollection. Keep in mind AnimeList2 is a temporary Collection.
var groupFinale = AnimeList2.GroupBy(txt => txt.id).Where(grouping => grouping.Count() > 1).ToObservableCollection();
GroupedAnimeByGenre = groupFinale ;
But I am unable to convert/use groupFinale with GroupedAnimeByGenre. I am missing the extension method part as I am not well aware of the syntax. Please help
If you remove the ToObservableCollection() call and take just that part
var groupFinale = AnimeList2.GroupBy(txt => txt.id).Where(grouping => grouping.Count() > 1);
you'll see that the type of groupFinale is IEnumerable<IGrouping<int, Anime>>. Hence applying ToObservableCollection() will result in ObservableCollection<IGrouping<int, Anime>>. However, the type of the GroupedAnimeByGenre is ObservableCollection<KeyedList<int, Anime>>. So you need to convert IEnumerable<IGrouping<int, Anime>> to IEnumerable<KeyedList<int, Anime>> which in LINQ is performed by the Select method.
Shortly, you can use something like this
var groupFinale = AnimeList2
.GroupBy(txt => txt.id)
.Where(grouping => grouping.Count() > 1)
.Select(grouping => new KeyedList<int, Anime>(grouping))
.ToObservableCollection();
You can make such conversion easier by providing an extension method (similar to BCL provided ToArray() / ToList()) that will allow skipping the type arguments like this
public static class KeyedList
{
public static KeyedList<TKey, TItem> ToKeyedList<TKey, TItem>(this IGrouping<TKey, TItem> source)
{
return new KeyedList<TKey, TItem>(source);
}
}
Then you can use simply
var groupFinale = AnimeList2
.GroupBy(txt => txt.id)
.Where(grouping => grouping.Count() > 1)
.Select(grouping => grouping.ToKeyedList())
.ToObservableCollection();
I am totally new to Web API an am not understanding how to Filter get calls.
This method returns all items in my database.
// GET: api/LogEntries
public IQueryable<LogEntry> GetLogEntries()
{
return db.LogEntries;
}
This method returns a specific item in my database.
// GET: api/LogEntries/5
[ResponseType(typeof(LogEntry))]
public IHttpActionResult GetLogEntry(int id)
{
LogEntry logEntry = db.LogEntries.Find(id);
if (logEntry == null)
{
return NotFound();
}
return Ok(logEntry);
}
So now I want to filter the returned records so I created this method but it won't work because the specific item method gets called. I seem to be missing a concept and am hoping you can point me to more clear understanding. Thanks
// GET: api/LogEntries
public IQueryable<LogEntry> GetLogEntries(string levelID)
{
int levIdInt;
if (Int32.TryParse(levelID, out levIdInt))
{
return db.LogEntries.Take(300).Where(l => (int)l.Level == levIdInt).OrderByDescending(d => d.TimeStamp);
}
return db.LogEntries.Where(i => i.ID < 0);
}
You need to specify the route for that method
[Route("api/LogEntries/Level/{levelID}"]
public IQueryable<LogEntry> GetLogEntries(string levelID)
{}
More on routing is available here http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
I need some help with this error "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."
It's a asp.net mvc3, EF4, and ms sql.
Here is the razor with two dropdowns:
<div class="editRow">
#Html.DropDownListFor(m=>m.IndustryId, (SelectList)ViewBag.Industry, #Empower.Resource.General.ddlDefaultVal, new { #class = "ddl400" })
#Html.ValidationMessageFor(m => m.IndustryId)
</div>
<div class="editRow">
#Html.DropDownListFor(m=>m.ProvinceId, (SelectList)ViewBag.Province, #Empower.Resource.General.ddlDefaultVal, new {#class = "ddl400"})
#Html.ValidationMessageFor(m => m.ProvinceId)
</div>
Controller:
IndustryService indService = new IndustryService();
ViewBag.Industry = new SelectList(indService.GetAllIndustry(), "IndustryId", "IndustryName");
ProvinceService proService = new ProvinceService();
ViewBag.Province = new SelectList(proService.GetAllProvince(), "ProvinceId", "ProvinceName");
return View();
ProvinceService:
public IEnumerable<Province> GetAllProvince()
{
using (var context = DBContext.ObjectContext)
{
var pros = context.Provinces;
return pros;
}
}
IndustryService is identical as above...
public class DBContext
{
private static EmpowerDBEntities _empowerContext;
public static EmpowerDBEntities ObjectContext
{
get
{
if (_empowerContext == null)
_empowerContext = new EmpowerDBEntities();
return _empowerContext;
}
}
}
I know the problem occurs in second dropdown when it tries to retrive data while the connection is desposed by previous query. Please help me with this, thanks.
The fix is simple - convert to a .ToList() or First() before using. LINQ has deferred execution and tries to run this command after the context is disposed (when your object results are referenced) - not when you actually make the call.. You need to force it to run now while the context is in scope.
using (var context = DBContext.ObjectContext)
{
var pros = context.Provinces;
return pros.ToList();
}
Also - your code above is checking for null in the get accessor. However this object won't be null - it will be disposed, so you cannot do your check this way, you need to check if its null and not disposed.
public class DBContext
{
private static EmpowerDBEntities _empowerContext;
public static EmpowerDBEntities ObjectContext
{
get
{
if (_empowerContext == null || _empowerContext.IsDisposed())
_empowerContext = new EmpowerDBEntities();
return _empowerContext;
}
}
}
something like that anyways :)
I ran into a similar problem. I had been following this pattern, which I had seen in many code examples on the web:
public ActionResult Show(int id)
{
using (var db = new MyDbContext())
{
var thing = db.Things.Find(id);
return View(thing);
}
}
However, this was causing the ObjectDisposedException listed above whenever I tried to access anything that hadn't been loaded into memory in the View code (in particular, one-to-many relationships in the main view model).
I found a different pattern in this example:
public class MyController : Controller
{
private MyDbContext _db;
public MyController()
{
_db = new MyDbContext();
}
public ActionResult Show(int id)
{
// Do work such as...
var thing = _db.Things.Find(id);
return View(thing);
}
protected override void Dispose(bool disposing)
{
_db.Dispose();
base.Dispose(disposing);
}
}
This pattern keeps the database connection alive until the View has finished rendering, and neatly disposes of it when the Controller itself is disposed.
Here is the problem when you are using
using(var context=new CustomerContext())
{
return View(context.Customers.ToList());
}
when the block of code executes all references are disposed that you are lazzy loading so that's why it is throwing this error.
so i used
return View(context.Customers.ToList()) directly it will work perfectly fine.