简体   繁体   English

除了的加载顺序

[英]loading sequence from except

I have these two tables. 我有这两张桌子。

var cardtagtable = (from u in db.CardTagTables
                    where u.FKCardTagID == cardtable.cardID
                    select u.CardTagName).ToList();

var tagtable = (from u in db.TagTables
                select u.TagName).ToList();

Where in cardtagtable all names are selected to match with tagtable 's name. cardtagtable所有名称都被选择为与tagtable的名称匹配。

Condition- 条件-

I want names from tagtable except names coming from cardtagtable list. 我想要来自tagtable名称,但来自cardtagtable列表的名称除外。

so I tried here- 所以我在这里尝试了

 var list = tagtable.Except(cardtagtable);

This list is a sequence of all names except from cardtagtable . list是除cardtagtable之外的所有名称的序列。

Everything is all right till now. 到现在为止一切都很好。

Now I will use this list and pass it through the model. 现在,我将使用此list并将其传递给模型。

var taglist = (from u in list
               select new TagModel {
                   tagId = u.TagID,
                   tagName = u.TagName,
                   tagCount = Convert.ToInt32(u.TagCount) == null ? 0 : Convert.ToInt32(u.TagCount),
               }).ToList();

But this query says me no definition found for Model values from u . 但是这个查询说我没有找到u Model值的定义。

How do I use this list here in this case? 在这种情况下,如何在此使用此列表?

Because u is probably a string because your list is a IEnumerable<string> .The reason is that you are only selecting your TagNames , so your list contains only tag names , not your TagTables .Instead you need to select your elements instead of just tag names : 因为u可能是一个string ,因为你的列表是一个IEnumerable<string> 。其原因是,你只选择你的TagNames ,所以你的列表只包含标记名称 ,而不是你TagTables。相反,你需要选择你的元素 ,而不仅仅是标签名称

var cardtagtable = (from u in db.CardTagTables
                    where u.FKCardTagID == cardtable.cardID
                    select u).ToList();

var tagtable = (from u in db.TagTables
                select u).ToList();

Then use Where and Any instead of Except like this: 然后使用Where and Any代替Except这样:

var list = tagtable.Where(c => !cardtagtable
                             .Any(x => x.CardTagName == c.TagName));

Then your last query should work fine. 然后,您的最后一个查询应该可以正常工作。

Update: Also there is a more elegant and optimized way to do that (especially if this is LINQ to SQL ).You can select only CardtagNames from cardtagtable and use Contains method: 更新:也有一个更优雅和优化的方式做到这一点(特别是如果这是LINQ to SQL )。你可以只选择CardtagNamescardtagtable和使用Contains方法:

var cardtagtable = (from u in db.CardTagTables
                    where u.FKCardTagID == cardtable.cardID
                    select u.CardTagName).ToList();

var tagtable = (from u in db.TagTables
                select u).ToList();

var list = tagtable.Where(c => !cardtagtable.Contains(c.TagName));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何忽略可观察序列中除错误以外的所有通知? - How to ignore all notifications from an observable sequence except errors? MvvmCross ShowViewModel加载序列 - MvvmCross ShowViewModel loading sequence 在Combobox上的数据加载,当前登录用户除外 - Data Loading on Combobox except current logging user 用户设置未加载,除调试模式外 - User Settings not loading, except in Debug mode 在可观察序列中删除除最后一个样本之外的任何样本,直到消费者准备好 - Drop any except last sample in observable sequence until consumer is ready 正则表达式 - 匹配除URL中的特定单词之外的任何字符序列 - Regex - Match any sequence of characters except a particular word in a URL 如何控制控件的加载顺序? - How can I control the loading sequence of controls? 动态加载工作正常,除非在可执行文件被 ILMerged 之后 - Dynamic loading working fine, except after the executable is ILMerged 本地序列不能在 LINQ to SQL 的查询运算符实现中使用,除了 Contains() 运算符 - Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator 除Contains运算符外,本地序列不能用于查询运算符的LINQ to SQL实现 - Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM