Linq 检查集合中值的相等性,并将值分配给其他集合
本文关键字:集合 分配 其他 检查 Linq | 更新日期: 2023-09-27 18:10:57
我有一个名为消息和历史记录的模型。历史还有另一种模式,即列表
消息模型具有如下属性,
MessageId,Title,Content,FileID,FileName
历史模型:
Title,Content,List<File>
文件模型:----这是历史模型中的列表
FileId,FileName
消息模型数据如下,邮件 ID 标题内容 文件 ID 文件名101
MessageId Title Content Fileid FileName
100 1st title 1st content 1 User.pdf
100 1st title 1st content 2 Log.txt
100 1st title 1st content 3 manual.doc
101 2nd title 2nd content 4 dummy.txt
102 3rd title 3rd content Null Null
103 4th title 4th content Null Null
场景:单个消息 ID 可以有多个文件 ID,文件名或没有文件 ID,文件名。
我想将消息标题,内容分配给历史模型标题和内容以及文件ID,文件名到文件模型列表的不同值
我的历史模型应具有如下值,
Title Content Fileid FileName
1st title 1st content 1 User.pdf
2 Log.txt
3 manual.doc
2nd title 2nd content 4 dummy.txt
3rd title 3rd content Null Null
4th title 4th content Null Null
Messageid 在这里是唯一的。我学会了用linq编写简单的查询。对于上述情况,任何人都可以建议如何做。
GroupBy。
假设messages
是 IEnumerable,以下内容应该可以满足您的需求:
var history = messages.GroupBy(m => new { m.Title, m.Content }, (group, data) => new HistoryModel()
{
Title = group.Title,
Content = group.Content,
Files = data.Select(k => new File()
{
FileId = k.Fileid,
FileName = k.FileName
}
).ToList(),
});
这基本上是说按消息的Title
和Content
属性对消息进行分组。对于每个组,lambda 称为传入传递group
,这是一种匿名类型,包含组的详细信息(即标题和内容(,data
是包含组中每个MessageModel
的IEnumerable<MessageModel>
。由此我们可以构建一个 HistoryModel
列表 .
操作后编辑问题
以上应该只给你 4 个元素 history
.我用来测试它的完整列表是:
class MessageModel
{
public string MessageId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string Fileid { get; set; }
public string FileName { get; set; }
public MessageModel(string m, string t, string c, string f, string f2)
{
MessageId = m;
Title = t;
Content = c;
Fileid = f;
FileName = f2;
}
}
class HistoryModel
{
public string Title { get; set; }
public string Content { get; set; }
public List<File> Files { get; set; }
}
class File
{
public string FileId { get; set; }
public string FileName { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<MessageModel> messages = new List<MessageModel>();
messages.Add(new MessageModel("100", "1st title", "1st content", "1", "User.pdf"));
messages.Add(new MessageModel("100", "1st title", "1st content", "2", "Log.txt"));
messages.Add(new MessageModel("100", "1st title", "1st content", "3", "manual.doc"));
messages.Add(new MessageModel("101", "2nd title", "2nd content", "4", "dummy.txt"));
messages.Add(new MessageModel("102", "3rd title", "3rd content", null, null));
messages.Add(new MessageModel("103", "4th title", "4th content", null, null));
var history = messages.GroupBy(m => new { m.Title, m.Content }, (group, data) => new HistoryModel()
{
Title = group.Title,
Content = group.Content,
Files = data.Select(k => new File()
{
FileId = k.Fileid,
FileName = k.FileName
}).ToList(),
});
foreach (var h in history)
{
Console.WriteLine(h.Title + " " + h.Content);
foreach (var file in h.Files)
{
Console.WriteLine("'t" + file.FileId + " " + file.FileName);
}
}
Console.ReadLine();
}
}
以上生成输出:
第一标题第一内容
1 个用户.pdf
2 日志.txt
3 手动.doc
第二标题第二内容
4个假人.txt
第三标题第三内容第四标题第四内容
可以使用 Group by 子句对公共属性(MessageId、Title 和 Content(进行分组。然后使用它作为 ke,然后可以使用 Select 子句来构建 HistoryModel。
下面的完整示例(请注意 .Dump(( 来自 LinqPad(
void Main()
{
var messages = new List<MessageModel>
{
new MessageModel(100,"1st title","1st content", 1,"User.pdf"),
new MessageModel(100,"1st title","1st content", 2,"Log.txt"),
new MessageModel(100,"1st title","1st content", 3,"manual.doc"),
new MessageModel(101,"2nd title","2nd content", 4,"dummy.txt"),
new MessageModel(102,"3rd title","3rd content" ),
new MessageModel(103,"3rd title","3rd content")
};
var groupedMessages =
messages.GroupBy(x=> new {x.MessageId, x.Title, x.Content})
.Select(x=> new HistoryModel
{
MessageId = x.Key.MessageId,
Title = x.Key.Title,
Content = x.Key.Content,
Files = new List<FileModel>(x.Where( f=> f.FileId != null)
.Select( g => new FileModel(g.FileId, g.FileName)))
});
groupedMessages.Dump();
}
// Define other methods and classes here
public class MessageModel
{
public int MessageId {get; set;}
public string Title {get; set;}
public string Content {get; set;}
public int? FileId {get; set;}
public string FileName {get; set;}
public MessageModel(int id, string title, string content, int? fileId = null, string fileName= null)
{
MessageId = id;
Title = title;
Content = content;
FileId = fileId;
FileName = fileName;
}
}
public class HistoryModel
{
public int MessageId {get; set;}
public string Title {get; set;}
public string Content {get; set;}
public List<FileModel> Files{get; set;} = new List<FileModel>();
}
public class FileModel
{
public int? FileId {get; set;}
public string FileName {get; set;}
public FileModel(int? id, string name)
{
FileId = id;
FileName = name;
}
}