English 中文(简体)
有条件地从两个列表合并数据
原标题:Merging data from two lists conditionally

有条件地从两个列表合并数据

表格结构如下

MobileID   ModelID   ManufacturerID    IsApproved

被批准的布林值,

当在数据库中完成新的移动条目时,它会由管理员批准,直到其IsApproved值为0,当它获得批准时,IsApproved值将变为1。

我想跟紧我

我正使用下面写的第一个查询 获得所有被批准的手机列表

问题是现在我要根据两个条件 显示所有手机

(1) 如果移动电话存在,其型号ID和制造商ID相同(意指规格得到更新),而IsApproved 字段将变成 0 - & gt;不在 DB 中,而仅用于显示目的的列表中)

(2) 如果移动电话是新获得的,则Is批准地位应为0。

我该怎么办才能有条件地合并这两个名单?

我的LINQ查询如下:

var listApproved = objMobile.MobileLists.where(mb => mb.IsApproved == true).toList();
var listUnApproved = objMobile.MobileLists.where(mb => mb.IsApproved == false).toList();

假设表数据如下:

MobileID    ModelID     ManufacturerID     IsApproved
01            mod1             manu1               1
02            mod2             manu2               1
03            mod3             manu3               1
04            mod1             manu1               0
05            mod5             manu5               0
06            mod6             manu6               0
07            mod2             manu2               0

我想把名单写成

MobileID    ModelID     ManufacturerID     IsApproved
01            mod1            manu1            0
02            mod2            manu2            0
03            mod3            manu3            1
05            mod5            manu5            0
05            mod6            manu6            0
最佳回答

先把名单存起来 省得我们不止一次拿到

var listOfAllMobiles = objMobile.MobileLists.ToList();

现在让我们用你的逻辑来获取所有的手机:

var mobileList = listOfAllMobiles
    .Select(m => new Mobile
                 {
                     IsApproved = m.IsApproved == true && !listOfAllMobiles
                         .Any(l => l.ModelID == m.ModelID
                             && l.ManufacturerID == m.ManufacturerID
                             && l.MobileID != m.MobileID),
                     MobileID = m.MobileID,
                     ModelID = m.ModelID,
                     ManufacturerID = m.ManufacturerID
                 })
    .ToList();

EDIT: 上述代码将设定正确的值, 但它不会用重复的 制造者id ModelID 来过滤移动。 您将为此另外使用一个 foreach , 例如 :

var displayMobileList = new List<Mobile>();

foreach(var mobile in mobileList
    .Where(m => !displayMobileList
        .Any(m2 => m2.ModelID == m.ModelID 
            && m2.ManufacturerID == m.ManufacturerID)))
{
    displayMobileList.Add(mobile);
}
问题回答

暂无回答




相关问题
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. ...

热门标签