English 中文(简体)
如何从收藏中查询实体而不使用前方
原标题:How to query entities from a collection without using foreach

我努力奋斗,试图把思想放在这些概念上,非常感激任何帮助。如果在一张桌子上有一套命令,那么最好的办法就是绕过这些命令,对其他表格进行查询。

这个循环可以做我想做的事,但是我被告知并展示了这如何造成业绩问题。在其他文章中,我读过最好在循环之外查询底部的地方。

foreach (var item in myOrders)
{
var lookUpValues=(from f in ORDERS
where f.ORDERS ==item.ORDER_ID 
&& f.ORDERS_SUFFIX==item.LINE
select f.Something);
}

而不是在我反复尝试过这样的事情之前就把整张顺序桌子拉下来, 但是正如你所见,它不会返回准确的行。

var orderIds=myOrders.Select (x =>x.ORDER_ID).Distinct().ToList();
var lineIds=myOrders.Select (x =>x.LINE).Distinct().ToList();

var query=(from f in ORDERS
where orderIds.Contains(f.ORDERS)
&& lineIds.Contains(f.ORDERS_SUFFIX)
select f).ToList();

下面是我认为需要的。 它会返回正确的命令, 需要它循环。 但是, 它会抛出一个错误, 我相信这是因为您可以一起查询内存对象和实体 。

var ordersAndLine=(from f in myOrders select new{sId=f.ORDER_ID,line=f.LINE}).ToList();

var query=(from f in ORDERS
from s in ordersAndLine
where s.sId==f.ORDERS
&& s.line==f.ORDERS_SUFFIX
select f).ToList();

不支持的例外: 无法创建恒定的匿名类型值 。 在此情况下只支持原始类型( 如 Int32、 String 和 General ) 。

我真的希望我的问题是有道理的。如果我没有正确解释我的情况,我提前道歉。我所做的是把大部分桌子拉下来,以回忆过去,但我担心这会造成业绩问题,并想看看是否有更好的办法过滤结果查询。

最佳回答

如果你把第一次尝试和最后一次尝试结合起来,你就会得到它。如果你不制造一种匿名类型,最后一次尝试就会好起来。

var ordersAndLine=(from f in myOrders select f).ToList();

var query=(from f in ORDERS
from s in ordersAndLine
where s.ORDER_ID==f.ORDERS
&& s.LINE==f.ORDERS_SUFFIX
select f).ToList();
问题回答

尝试此 :

var orders = myOrders.SelectMany(item => (from f in ORDERS
                                          where f.ORDERS ==item.ORDER_ID 
                                          && f.ORDERS_SUFFIX==item.LINE
                                          select f.Something));




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签