简体   繁体   中英

Entity Framework Projection Crashing VS

I have a lines table, and a tags table. Each line may have multiple tags from the tags table. Im trying to build a checklist, with all the tags, and have a checkbox that shows if that line has that tag, if it does its checked.

Ive set the datagrid view with 2 columns. one bound to Name and another checkboxcell bound to IsTagged

Im trying to use a projection for this :

-> line comes into constructor
var tagsList = from t in rs.Tags select new { Name = t.Name, IsTagged = line.Tags.Where(x => x.Name == t.Name).Any() };
dgvTags.DataSource = tagsList;

My entity diagram is as below :

在此处输入图片说明

UPDATE :

I re-did the whole thing, upgraded the back db to 2008r2 from 2005, now it seems to work but gives the following error... which doesnt crash but shows no items in the datagrid.

在此处输入图片说明

I think the problem is due to using the line parameter's collection in your linq-to-entities query.

I would try rewriting the query to only use the primitive types like this:

var tagNames = line.Tags.Select(x => x.Name).ToList();
var tagsList = from t in rs.Tags 
               join n in tagNames on t.Name equals n into tags
               select new 
               {
                 Name = t.Name, 
                 IsTagged = tags.Any() 
               };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM